<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width,initial-scale=1" />
    <title>玩转前端面试之JS | aiyoudiao</title>
    <meta name="generator" content="VuePress 1.9.10" />
    <link rel="icon" href="/img/blog.ico">
    <script src="https://cdn.jsdelivr.net/npm/live2d-widget@3.1.4/lib/L2Dwidget.min.js"></script> <meta name="description" content="码二~">
    <meta name="keywords" content="前端博客,个人技术博客,前端,前端开发,前端框架,web前端,前端面试题,技术文档,学习,面试,JavaScript,js,ES6,TypeScript,vue,python,css3,html5,Node,git,github,gitee,markdown">
    <meta name="theme-color" content="#11a8cd">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no"> <link rel="preload" href="/assets/css/0.styles.146197cf.css" as="style"><link rel="preload" href="/assets/js/app.bd2fbc77.js" as="script"><link rel="preload" href="/assets/js/3.72c9c947.js" as="script"><link rel="preload" href="/assets/js/100.20d2348f.js" as="script"><link rel="preload" href="/assets/js/42.4251ca36.js" as="script"><link rel="prefetch" href="/assets/js/1.4ed4671d.js"><link rel="prefetch" href="/assets/js/10.bd6ddb58.js"><link rel="prefetch" href="/assets/js/101.ba7b784c.js"><link rel="prefetch" href="/assets/js/102.c3e2dcae.js"><link rel="prefetch" href="/assets/js/103.0f4c50f3.js"><link rel="prefetch" href="/assets/js/104.ef47a111.js"><link rel="prefetch" href="/assets/js/105.2e00f516.js"><link rel="prefetch" href="/assets/js/106.b50e19b9.js"><link rel="prefetch" href="/assets/js/107.e125a8f6.js"><link rel="prefetch" href="/assets/js/108.770493ab.js"><link rel="prefetch" href="/assets/js/109.74766d7b.js"><link rel="prefetch" href="/assets/js/11.f786a5ee.js"><link rel="prefetch" href="/assets/js/110.0b0ee5b4.js"><link rel="prefetch" href="/assets/js/111.835b0e44.js"><link rel="prefetch" href="/assets/js/112.352fa217.js"><link rel="prefetch" href="/assets/js/113.4e908557.js"><link rel="prefetch" href="/assets/js/114.7b77996d.js"><link rel="prefetch" href="/assets/js/115.bdc61268.js"><link rel="prefetch" href="/assets/js/116.d5da9b8b.js"><link rel="prefetch" href="/assets/js/117.35ab1f9f.js"><link rel="prefetch" href="/assets/js/118.517c151d.js"><link rel="prefetch" href="/assets/js/119.f7f49ba8.js"><link rel="prefetch" href="/assets/js/12.3c729a65.js"><link rel="prefetch" href="/assets/js/120.b559598b.js"><link rel="prefetch" href="/assets/js/121.bf8a2f43.js"><link rel="prefetch" href="/assets/js/122.11a0bc97.js"><link rel="prefetch" href="/assets/js/123.2bafdde7.js"><link rel="prefetch" href="/assets/js/124.dc393688.js"><link rel="prefetch" href="/assets/js/125.ed3f389a.js"><link rel="prefetch" href="/assets/js/126.8fd9a57d.js"><link rel="prefetch" href="/assets/js/127.3bf2a1f2.js"><link rel="prefetch" href="/assets/js/128.b9c671d3.js"><link rel="prefetch" href="/assets/js/129.5d331f0d.js"><link rel="prefetch" href="/assets/js/13.7b1a1fe5.js"><link rel="prefetch" href="/assets/js/130.53e4f9c6.js"><link rel="prefetch" href="/assets/js/131.dcc47e1d.js"><link rel="prefetch" href="/assets/js/132.692dcdcd.js"><link rel="prefetch" href="/assets/js/133.e293202c.js"><link rel="prefetch" href="/assets/js/134.593dccf2.js"><link rel="prefetch" href="/assets/js/135.d76d384b.js"><link rel="prefetch" href="/assets/js/136.a519c23c.js"><link rel="prefetch" href="/assets/js/137.b1821288.js"><link rel="prefetch" href="/assets/js/138.5bcea4ef.js"><link rel="prefetch" href="/assets/js/139.076664b0.js"><link rel="prefetch" href="/assets/js/14.35f257b2.js"><link rel="prefetch" href="/assets/js/140.a019e655.js"><link rel="prefetch" href="/assets/js/141.1f70e1c7.js"><link rel="prefetch" href="/assets/js/142.5ed728fd.js"><link rel="prefetch" href="/assets/js/143.1c8cdc78.js"><link rel="prefetch" href="/assets/js/144.b0cb125b.js"><link rel="prefetch" href="/assets/js/145.c0209a76.js"><link rel="prefetch" href="/assets/js/146.551469f4.js"><link rel="prefetch" href="/assets/js/147.1dfd721d.js"><link rel="prefetch" href="/assets/js/148.91d07ef5.js"><link rel="prefetch" href="/assets/js/149.5b88b710.js"><link rel="prefetch" href="/assets/js/15.23bbc29a.js"><link rel="prefetch" href="/assets/js/150.8301107f.js"><link rel="prefetch" href="/assets/js/151.867da089.js"><link rel="prefetch" href="/assets/js/152.935d5046.js"><link rel="prefetch" href="/assets/js/153.f39d8435.js"><link rel="prefetch" href="/assets/js/154.6b9eb2c3.js"><link rel="prefetch" href="/assets/js/155.14283ad4.js"><link rel="prefetch" href="/assets/js/156.2d7c1a2a.js"><link rel="prefetch" href="/assets/js/157.2f28d02f.js"><link rel="prefetch" href="/assets/js/158.151221ae.js"><link rel="prefetch" href="/assets/js/159.ef6d7ffe.js"><link rel="prefetch" href="/assets/js/16.1793aef7.js"><link rel="prefetch" href="/assets/js/160.de54c4ea.js"><link rel="prefetch" href="/assets/js/161.24d4e57c.js"><link rel="prefetch" href="/assets/js/162.632032fe.js"><link rel="prefetch" href="/assets/js/163.fd01cd99.js"><link rel="prefetch" href="/assets/js/164.45f203f5.js"><link rel="prefetch" href="/assets/js/165.aafe4fe1.js"><link rel="prefetch" href="/assets/js/166.1dd1d21c.js"><link rel="prefetch" href="/assets/js/167.5501b3a1.js"><link rel="prefetch" href="/assets/js/168.fbe58b1f.js"><link rel="prefetch" href="/assets/js/169.2cae7f5e.js"><link rel="prefetch" href="/assets/js/17.bbfe63f2.js"><link rel="prefetch" href="/assets/js/170.265f7c9e.js"><link rel="prefetch" href="/assets/js/171.b61f327d.js"><link rel="prefetch" href="/assets/js/172.5d0043fd.js"><link rel="prefetch" href="/assets/js/173.45284bb6.js"><link rel="prefetch" href="/assets/js/174.9130e0c4.js"><link rel="prefetch" href="/assets/js/175.2b38bddd.js"><link rel="prefetch" href="/assets/js/176.9772cf09.js"><link rel="prefetch" href="/assets/js/177.69048ebc.js"><link rel="prefetch" href="/assets/js/178.e10d7ce5.js"><link rel="prefetch" href="/assets/js/179.3789edc0.js"><link rel="prefetch" href="/assets/js/18.0807ded0.js"><link rel="prefetch" href="/assets/js/180.ab675e47.js"><link rel="prefetch" href="/assets/js/181.2e39eff0.js"><link rel="prefetch" href="/assets/js/19.becf5a76.js"><link rel="prefetch" href="/assets/js/2.eb089a4f.js"><link rel="prefetch" href="/assets/js/20.cea59652.js"><link rel="prefetch" href="/assets/js/21.58c43ff1.js"><link rel="prefetch" href="/assets/js/22.f73b825d.js"><link rel="prefetch" href="/assets/js/23.43b13730.js"><link rel="prefetch" href="/assets/js/24.f77f93ca.js"><link rel="prefetch" href="/assets/js/25.7dfaf3fb.js"><link rel="prefetch" href="/assets/js/26.629d28e5.js"><link rel="prefetch" href="/assets/js/27.4fff23ea.js"><link rel="prefetch" href="/assets/js/28.1b8ae389.js"><link rel="prefetch" href="/assets/js/29.d5cce9a0.js"><link rel="prefetch" href="/assets/js/30.961d5519.js"><link rel="prefetch" href="/assets/js/31.121dd1af.js"><link rel="prefetch" href="/assets/js/32.4a3c5df7.js"><link rel="prefetch" href="/assets/js/33.5537f44b.js"><link rel="prefetch" href="/assets/js/34.1d4d4653.js"><link rel="prefetch" href="/assets/js/35.d094209b.js"><link rel="prefetch" href="/assets/js/36.832660c5.js"><link rel="prefetch" href="/assets/js/37.145c3665.js"><link rel="prefetch" href="/assets/js/38.4f369bfe.js"><link rel="prefetch" href="/assets/js/39.ba060044.js"><link rel="prefetch" href="/assets/js/4.66d742f6.js"><link rel="prefetch" href="/assets/js/40.e50e0379.js"><link rel="prefetch" href="/assets/js/41.4ed7617c.js"><link rel="prefetch" href="/assets/js/43.d22b74c4.js"><link rel="prefetch" href="/assets/js/44.59439f9d.js"><link rel="prefetch" href="/assets/js/45.da28bc46.js"><link rel="prefetch" href="/assets/js/46.b8db1176.js"><link rel="prefetch" href="/assets/js/47.7ed16fc7.js"><link rel="prefetch" href="/assets/js/48.c982d5ed.js"><link rel="prefetch" href="/assets/js/49.a7579f55.js"><link rel="prefetch" href="/assets/js/5.08802d7d.js"><link rel="prefetch" href="/assets/js/50.103b5bf6.js"><link rel="prefetch" href="/assets/js/51.0fe9d79a.js"><link rel="prefetch" href="/assets/js/52.9ba31e26.js"><link rel="prefetch" href="/assets/js/53.0e8bc1f0.js"><link rel="prefetch" href="/assets/js/54.9566e517.js"><link rel="prefetch" href="/assets/js/55.a124abae.js"><link rel="prefetch" href="/assets/js/56.d9cf0800.js"><link rel="prefetch" href="/assets/js/57.93599da0.js"><link rel="prefetch" href="/assets/js/58.d943f85b.js"><link rel="prefetch" href="/assets/js/59.50a66488.js"><link rel="prefetch" href="/assets/js/6.a3ea60eb.js"><link rel="prefetch" href="/assets/js/60.21aa3aa3.js"><link rel="prefetch" href="/assets/js/61.6712c00f.js"><link rel="prefetch" href="/assets/js/62.eff3e4b1.js"><link rel="prefetch" href="/assets/js/63.09701d5a.js"><link rel="prefetch" href="/assets/js/64.eb440dec.js"><link rel="prefetch" href="/assets/js/65.aeed0579.js"><link rel="prefetch" href="/assets/js/66.97244c64.js"><link rel="prefetch" href="/assets/js/67.e01c5c24.js"><link rel="prefetch" href="/assets/js/68.21be91ba.js"><link rel="prefetch" href="/assets/js/69.c0849905.js"><link rel="prefetch" href="/assets/js/7.7fd40e91.js"><link rel="prefetch" href="/assets/js/70.b32bbe5d.js"><link rel="prefetch" href="/assets/js/71.0efbc0c7.js"><link rel="prefetch" href="/assets/js/72.ef963181.js"><link rel="prefetch" href="/assets/js/73.ca7dd5db.js"><link rel="prefetch" href="/assets/js/74.4483ede8.js"><link rel="prefetch" href="/assets/js/75.374ab483.js"><link rel="prefetch" href="/assets/js/76.b4a39f08.js"><link rel="prefetch" href="/assets/js/77.6b30c3cd.js"><link rel="prefetch" href="/assets/js/78.15376c33.js"><link rel="prefetch" href="/assets/js/79.3153fcec.js"><link rel="prefetch" href="/assets/js/80.9a88c684.js"><link rel="prefetch" href="/assets/js/81.1e3f842c.js"><link rel="prefetch" href="/assets/js/82.996dbd3d.js"><link rel="prefetch" href="/assets/js/83.955158bf.js"><link rel="prefetch" href="/assets/js/84.71bdc76d.js"><link rel="prefetch" href="/assets/js/85.774e49f2.js"><link rel="prefetch" href="/assets/js/86.bebf32e5.js"><link rel="prefetch" href="/assets/js/87.becdbde1.js"><link rel="prefetch" href="/assets/js/88.49e933f4.js"><link rel="prefetch" href="/assets/js/89.eeceedfd.js"><link rel="prefetch" href="/assets/js/90.3ea6dd12.js"><link rel="prefetch" href="/assets/js/91.62a6a556.js"><link rel="prefetch" href="/assets/js/92.e2ebb8f5.js"><link rel="prefetch" href="/assets/js/93.dcdefe7a.js"><link rel="prefetch" href="/assets/js/94.bf412146.js"><link rel="prefetch" href="/assets/js/95.8deadcdc.js"><link rel="prefetch" href="/assets/js/96.9977087a.js"><link rel="prefetch" href="/assets/js/97.6591f9da.js"><link rel="prefetch" href="/assets/js/98.4db7f75e.js"><link rel="prefetch" href="/assets/js/99.a61462e9.js"><link rel="prefetch" href="/assets/js/vendors~docsearch.2852b102.js"> <link rel="stylesheet" href="/assets/css/0.styles.146197cf.css">
  </head>
  <body class="theme-mode-light">
    <div id="app" data-server-rendered="true"><div class="theme-container sidebar-open have-rightmenu"><header class="navbar blur"><div title="目录" class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" width="50" height="50" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/" class="home-link router-link-active"><img src="https://p3-passport.byteacctimg.com/img/user-avatar/794fdae4ff249d532da19a3c26d420ed~300x300.image" alt="aiyoudiao" class="logo"> <span class="site-name can-hide">
      aiyoudiao
    </span></a> <div class="links"><div class="sky-switch" data-v-3a03d589><label for="toggle" data-v-3a03d589><input id="toggle" type="checkbox" data-v-3a03d589><div data-v-3a03d589></div></label></div> <div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="笔记" class="dropdown-title"><!----> <span class="title" style="display:;">笔记</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/pages/84633490449/" class="nav-link">
  JavaScript
</a></li><li class="dropdown-item"><!----> <a href="/pages/2331001041/" class="nav-link">
  Vue
</a></li><li class="dropdown-item"><!----> <a href="/pages/18114480448/" class="nav-link">
  React
</a></li><li class="dropdown-item"><!----> <a href="/pages/25236260426/" class="nav-link">
  低代码
</a></li><li class="dropdown-item"><!----> <a href="/pages/35345230523/" class="nav-link">
  线性系统
</a></li><li class="dropdown-item"><!----> <a href="/pages/08313561056/" class="nav-link">
  暂未分类
</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="算法与设计" class="dropdown-title"><!----> <span class="title" style="display:;">算法与设计</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/pages/70741550255/" class="nav-link">
  LeetCode
</a></li><li class="dropdown-item"><!----> <a href="/pages/17845450445/" class="nav-link">
  算法
</a></li><li class="dropdown-item"><!----> <a href="/pages/90132170217/" class="nav-link">
  数据结构
</a></li><li class="dropdown-item"><!----> <a href="/pages/50546120212/" class="nav-link">
  设计模式
</a></li><li class="dropdown-item"><!----> <a href="/pages/02344550255/" class="nav-link">
  Other
</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="技能" class="dropdown-title"><!----> <span class="title" style="display:;">技能</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/pages/82158160216/" class="nav-link">
  PMP
</a></li><li class="dropdown-item"><!----> <a href="/pages/41858590259/" class="nav-link">
  Office
</a></li><li class="dropdown-item"><!----> <a href="/pages/02359360236/" class="nav-link">
  面试
</a></li><li class="dropdown-item"><!----> <a href="/pages/73600130213/" class="nav-link">
  Bash
</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="历程" class="dropdown-title"><!----> <span class="title" style="display:;">历程</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/pages/83857320232/" class="nav-link">
  流年往事
</a></li><li class="dropdown-item"><!----> <a href="/pages/93419130213/" class="nav-link">
  经验片段
</a></li><li class="dropdown-item"><!----> <a href="/pages/99744220322/" class="nav-link">
  读书杂感
</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="首页" class="dropdown-title"><!----> <span class="title" style="display:;">首页</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/archives/" class="nav-link">
  归档
</a></li><li class="dropdown-item"><!----> <a href="/categories/" class="nav-link">
  分类
</a></li><li class="dropdown-item"><!----> <a href="/tags/" class="nav-link">
  标签
</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="其它" class="dropdown-title"><!----> <span class="title" style="display:;">其它</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/pages/02657130213/" class="nav-link">
  简介
</a></li><li class="dropdown-item"><!----> <a href="/pages/5390102042/" class="nav-link">
  收藏
</a></li><li class="dropdown-item"><!----> <a href="/pages/32309510451/" class="nav-link">
  有趣
</a></li><li class="dropdown-item"><!----> <a href="/pages/23313210521/" class="nav-link">
  文档
</a></li></ul></div></div> <!----></nav></div></header> <div class="sidebar-mask"></div> <div class="sidebar-hover-trigger"></div> <aside class="sidebar" style="display:none;"><div class="blogger"><img src="/img/mar.jpg"> <div class="blogger-info"><h3>码二</h3> <span>扫微信二维码，认识一下码二吧😉。</span></div></div> <nav class="nav-links"><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="笔记" class="dropdown-title"><!----> <span class="title" style="display:;">笔记</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/pages/84633490449/" class="nav-link">
  JavaScript
</a></li><li class="dropdown-item"><!----> <a href="/pages/2331001041/" class="nav-link">
  Vue
</a></li><li class="dropdown-item"><!----> <a href="/pages/18114480448/" class="nav-link">
  React
</a></li><li class="dropdown-item"><!----> <a href="/pages/25236260426/" class="nav-link">
  低代码
</a></li><li class="dropdown-item"><!----> <a href="/pages/35345230523/" class="nav-link">
  线性系统
</a></li><li class="dropdown-item"><!----> <a href="/pages/08313561056/" class="nav-link">
  暂未分类
</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="算法与设计" class="dropdown-title"><!----> <span class="title" style="display:;">算法与设计</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/pages/70741550255/" class="nav-link">
  LeetCode
</a></li><li class="dropdown-item"><!----> <a href="/pages/17845450445/" class="nav-link">
  算法
</a></li><li class="dropdown-item"><!----> <a href="/pages/90132170217/" class="nav-link">
  数据结构
</a></li><li class="dropdown-item"><!----> <a href="/pages/50546120212/" class="nav-link">
  设计模式
</a></li><li class="dropdown-item"><!----> <a href="/pages/02344550255/" class="nav-link">
  Other
</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="技能" class="dropdown-title"><!----> <span class="title" style="display:;">技能</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/pages/82158160216/" class="nav-link">
  PMP
</a></li><li class="dropdown-item"><!----> <a href="/pages/41858590259/" class="nav-link">
  Office
</a></li><li class="dropdown-item"><!----> <a href="/pages/02359360236/" class="nav-link">
  面试
</a></li><li class="dropdown-item"><!----> <a href="/pages/73600130213/" class="nav-link">
  Bash
</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="历程" class="dropdown-title"><!----> <span class="title" style="display:;">历程</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/pages/83857320232/" class="nav-link">
  流年往事
</a></li><li class="dropdown-item"><!----> <a href="/pages/93419130213/" class="nav-link">
  经验片段
</a></li><li class="dropdown-item"><!----> <a href="/pages/99744220322/" class="nav-link">
  读书杂感
</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="首页" class="dropdown-title"><!----> <span class="title" style="display:;">首页</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/archives/" class="nav-link">
  归档
</a></li><li class="dropdown-item"><!----> <a href="/categories/" class="nav-link">
  分类
</a></li><li class="dropdown-item"><!----> <a href="/tags/" class="nav-link">
  标签
</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="其它" class="dropdown-title"><!----> <span class="title" style="display:;">其它</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/pages/02657130213/" class="nav-link">
  简介
</a></li><li class="dropdown-item"><!----> <a href="/pages/5390102042/" class="nav-link">
  收藏
</a></li><li class="dropdown-item"><!----> <a href="/pages/32309510451/" class="nav-link">
  有趣
</a></li><li class="dropdown-item"><!----> <a href="/pages/23313210521/" class="nav-link">
  文档
</a></li></ul></div></div> <!----></nav>  <ul class="sidebar-links"><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>PMP</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>Office</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading open"><span>面试</span> <span class="arrow down"></span></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/pages/02359360236/" class="sidebar-link">面试复盘</a></li><li><a href="/pages/14337520552/" class="sidebar-link">玩转前端面试之CSS</a></li><li><a href="/pages/3840807077/" aria-current="page" class="active sidebar-link">玩转前端面试之JS</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/pages/3840807077/#前言" class="sidebar-link">前言</a></li><li class="sidebar-sub-header"><a href="/pages/3840807077/#常见且容易忽视的作用域" class="sidebar-link">常见且容易忽视的作用域</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/pages/3840807077/#作用域链" class="sidebar-link">作用域链</a></li><li class="sidebar-sub-header"><a href="/pages/3840807077/#老说长谈的闭包" class="sidebar-link">老说长谈的闭包</a></li></ul></li><li class="sidebar-sub-header"><a href="/pages/3840807077/#js中的面向对象" class="sidebar-link">js中的面向对象</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/pages/3840807077/#老说长谈的面向对象" class="sidebar-link">老说长谈的面向对象</a></li><li class="sidebar-sub-header"><a href="/pages/3840807077/#封装对象" class="sidebar-link">封装对象</a></li><li class="sidebar-sub-header"><a href="/pages/3840807077/#继承" class="sidebar-link">继承</a></li><li class="sidebar-sub-header"><a href="/pages/3840807077/#多态" class="sidebar-link">多态</a></li><li class="sidebar-sub-header"><a href="/pages/3840807077/#箭头函数" class="sidebar-link">箭头函数</a></li><li class="sidebar-sub-header"><a href="/pages/3840807077/#创建对象的多种方式" class="sidebar-link">创建对象的多种方式</a></li><li class="sidebar-sub-header"><a href="/pages/3840807077/#继承的多种方式" class="sidebar-link">继承的多种方式</a></li><li class="sidebar-sub-header"><a href="/pages/3840807077/#对象克隆" class="sidebar-link">对象克隆</a></li></ul></li><li class="sidebar-sub-header"><a href="/pages/3840807077/#es6" class="sidebar-link">ES6</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/pages/3840807077/#简单知识点" class="sidebar-link">简单知识点</a></li><li class="sidebar-sub-header"><a href="/pages/3840807077/#class-与-它的继承原理" class="sidebar-link">class 与 它的继承原理</a></li><li class="sidebar-sub-header"><a href="/pages/3840807077/#promise-以及-它的扩展" class="sidebar-link">Promise 以及 它的扩展</a></li><li class="sidebar-sub-header"><a href="/pages/3840807077/#promise-状态机" class="sidebar-link">Promise 状态机</a></li></ul></li><li class="sidebar-sub-header"><a href="/pages/3840807077/#总结" class="sidebar-link">总结</a></li></ul></li></ul></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>Bash</span> <span class="arrow right"></span></p> <!----></section></li></ul> </aside> <div><main class="page"> <div class="theme-vdoing-wrapper bg-style-1"><div class="articleInfo-wrap" data-v-18fb2c02><div class="articleInfo" data-v-18fb2c02><ul class="breadcrumbs" data-v-18fb2c02><li data-v-18fb2c02><a href="/" title="首页" class="iconfont icon-home router-link-active" data-v-18fb2c02></a></li> <li data-v-18fb2c02><a href="/categories/?category=%E6%8A%80%E8%83%BD" title="分类" data-v-18fb2c02>
          技能
        </a></li> <li data-v-18fb2c02><a href="/categories/?category=%E9%9D%A2%E8%AF%95" title="分类" data-v-18fb2c02>
          面试
        </a></li> <!----></ul> <div class="info" data-v-18fb2c02><div title="作者" class="author iconfont icon-touxiang" data-v-18fb2c02><a href="javascript:;" data-v-18fb2c02>aiyoudiao</a></div> <div title="创建时间" class="date iconfont icon-riqi" data-v-18fb2c02><a href="javascript:;" data-v-18fb2c02>2022-07-15</a></div> <!----></div></div></div> <!----> <div class="content-wrapper"><div class="right-menu-wrapper"><div class="right-menu-margin"><div class="right-menu-content"></div></div></div> <h1><!----> <span>
            玩转前端面试之JS
          </span></h1> <div class="theme-vdoing-content content__default"><h2 id="前言"><a href="#前言" class="header-anchor">#</a> 前言</h2> <p>前端面试 无非就是 CSS + JS + 框架 + 工具 + 源码 + 算法 + 职业规划 + 实战，这篇文章以及接下来的文章也是围绕这些内容依次展开。</p> <p>说到JS，它非常的强大，除了在页面中运行js，还有在服务器中运行的node.js，以js的构建工具等等，但是我在这篇文章中并不会去说那些js的扩展，比如多端应用、服务器端框架部分、小程序等等东西，还是说说通用的以及基础的部分吧。</p> <p>首当其冲的是 作用域、闭包、面向对象的this指向以及多种创建方式呀继承方式呀、ES6的语法以及promise和async呀await呀，还有与HTML有关的JS DOM呀，和网络有关的 HTTP、NodeJS呀，和JS构建工具有关的Webpack呀，还有较为常用的JS超集TypeScript等等。</p> <h2 id="常见且容易忽视的作用域"><a href="#常见且容易忽视的作用域" class="header-anchor">#</a> 常见且容易忽视的作用域</h2> <p>作用域就一句话理解：它是变量的可用范围，用于防止不同范围的变量之间互相干扰。</p> <p>js底层只有全局作用域和函数作用域。es6中的块儿级作用域其实还是函数作用域，那是个自调用的闭包。<br>
全局作用域：非任意函数的外部范围<br>
全局变量：存储到全局作用域中的变量<br>
函数作用域：某个函数的内部范围，也叫做某个函数内的作用范围，也就是在函数的<code>{}</code>中形成的作用域<br>
局部变量：存储到函数作用域中的变量，不光是函数的<code>{}</code>中创建的变量，还有函数的<code>()</code>中声明的变量</p> <p>全局变量的好处与坏处与局部变量相对。全局变量的好处是可重复使用，局部变量与其相反。全局变量的坏处是容易造成全局污染，局部变量与其相反。</p> <p>除函数的<code>{}</code>中形成的作用域之外，其它非函数的<code>{}</code>都不是作用域，块级作用域实际上是用匿名函数自调来模拟的。<br>
而且js中是没有块级作用域的，那种看起来像强类型语言中的块级作用域的是需要配合let以及const来同时使用，如果没有它们，是无法拦住内部的变量超出<code>{}</code>的范围的。</p> <h3 id="作用域链"><a href="#作用域链" class="header-anchor">#</a> 作用域链</h3> <p>它是一个抽象的概念，指的是一个查找变量的路径。
一个函数在定义时就会创建好一条作用域链。在闭包中，内层函数会在被变量接收时，会创建好一条作用域链。<br>
当前函数内作用域 --&gt; 当前函数外部作用域 --&gt; 全局作用域。<br>
既然称之为链，自然就会有节点了，节点就像是域，它存储了这个域中包含的数据。<br>
逻辑上就像是一个链表，当你拿到链表的某一个节点时，就能往上或者往下追溯，从而找到其它节点的存储的数据了。
就像浏览器的源码调试栏中的作用域这一栏，里面会有全局 和 本地的作用域，全局 是指 window对象，本地 是指 函数作用域下的 变量。</p> <p>全局作用域、函数作用域、作用域链它们全都是对象。函数作用域是一个<strong>临时的对象</strong>，函数调用完毕之后，这个对象就会被回收、释放掉，闭包的使用就可能会使得这些临时对象不会被回收掉，从而容易内存泄露从而引发内存溢出。</p> <p><strong>注意</strong><br>
在js中给未声明的变量赋值，不会报错，而是自动挂载到window上了，也就是创建了一个全局变量并赋值。<br>
变量提升只会把声明提前，变量提升不会超过当前函数作用域，在严格模式下不声明的赋值会报错。<br>
js中只有两种局部变量，一个是在函数中用var声明的变量，一个是函数<code>()</code>中声明的形参。</p> <p><strong>函数调用过程</strong><br>
先创建函数作用域对象，然后保存局部变量，之后依次执行函数内的代码逻辑，最后函数执行完毕，正常销毁作用域对象。</p> <p><strong>function也是对象</strong><br>
js声明的function 等于 new Function，所以函数也是对象。</p> <h3 id="老说长谈的闭包"><a href="#老说长谈的闭包" class="header-anchor">#</a> 老说长谈的闭包</h3> <p>闭包虽然可能会导致内存泄露从而引发内存溢出，但它能解决全局变量极易被污染的问题。</p> <p>全局变量和局部变量的优缺点对立，而闭包能够同时兼备它俩的优点。</p> <p>闭包是即重用变量又保护变量不被污染的一种编程方式。</p> <p><strong>三步成闭包</strong></p> <ol><li>外层函数包裹内层函数和要保护的变量</li> <li>外层函数内部返回内层函数对象</li> <li>调用外层函数，用变量接收返回的内层函数对象</li></ol> <p>原理：外层函数调用后，外层函数的作用域对象 被返回的内层函数的作用域链引用着，无法释放，就形成了闭包对象。<br>
释放闭包：将接收内层函数的变量赋值为null时，该闭包就会被释放。</p> <p>在闭包中，接收内层函数对象的方式除了常规的那种，还有以下两种：</p> <ol><li>接收内层函数也可以在内部接收，内部接收直接赋值给window，比如在外部函数被调用时，在外层函数的内部就把内部函数赋值给了window。</li> <li>不光可以在外层函数内部返回内层函数对象，还可以将内层函数对象包裹成对象或者数组，然后再返回。</li></ol> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">function</span> <span class="token function">mother</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
   <span class="token keyword">var</span> a <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
   window<span class="token punctuation">.</span><span class="token function-variable function">fn</span> <span class="token operator">=</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> a<span class="token operator">++</span><span class="token punctuation">;</span> console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>a<span class="token punctuation">)</span><span class="token punctuation">}</span>
<span class="token punctuation">}</span>
<span class="token function">mother</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token comment">// 也能形成一个闭包</span>
</code></pre></div><p>之所以闭包会导致内存泄漏，是因为函数只有在<code>被调用的时候才会执行函数体中的内容</code>，而函数体中内容被执行的时候<code>会创建作用域对象</code>，当返回内层函数时，外层函数创建的<code>作用域对象并不会销毁掉</code>，于是只要内层函数被引用着，这个作用域对象就一直存在。<br>
那么如果一直执行外层函数，就会不停创建新的作用域对象，然后返回内层函数还一直被引用着，那么内存就会不停的上涨，最终内存溢出。</p> <p>如果你想查看内层函数有哪些作用域对象，可以通过以下代码查看。</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">function</span> <span class="token function">outerFn</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">var</span> value <span class="token operator">=</span> <span class="token number">999</span><span class="token punctuation">;</span>
    <span class="token keyword">return</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>value<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token keyword">const</span> innerFn <span class="token operator">=</span> <span class="token function">outerFn</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
console<span class="token punctuation">.</span><span class="token function">dir</span><span class="token punctuation">(</span>innerFn<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 查看这个函数对象的[[Scopes]]隐藏属性</span>
</code></pre></div><p><img src="https://cdn.jsdelivr.net/gh/filess/img8@main/2022/07/16/1657906633145-9d9b81be-22fe-4b09-a2ed-9fcf299cb164.png" alt=""></p> <p><strong>解闭包题</strong></p> <p>管理员创建用户，并把权限分配给用户，用户可以使用分配到的权限。<br>
管理员可以创建多个用户，用户被销毁后，分配给用户的权限也会被回收掉。<br>
解决闭包问题需要找到以下三种东西。</p> <ol><li>外层函数 // 管理员</li> <li>外层函数的局部变量 // 权限(钥匙)</li> <li>内层函数(可能有多个) // 用户</li></ol> <h2 id="js中的面向对象"><a href="#js中的面向对象" class="header-anchor">#</a> js中的面向对象</h2> <p>面向对象编程便于大量数据的管理维护。</p> <h3 id="老说长谈的面向对象"><a href="#老说长谈的面向对象" class="header-anchor">#</a> 老说长谈的面向对象</h3> <p>封装：根据设计，集中去的保存某类事物的数据以及功能，将零散的数据和功能分门别类的进行适合的整理，从而便于维护和管理。<br>
继承：复用封装好的功能代码，减少不必要的冗余代码，提升代码的质量以及美感。<br>
多态：根据预定接口，对该接口进行不同的功能实现，使用的时候通过切换不同的对象，从而实现不同的功能效果。</p> <h3 id="封装对象"><a href="#封装对象" class="header-anchor">#</a> 封装对象</h3> <p><strong>第一种</strong><br>
使用对象字面量<code>{}</code>，虽然它有<code>{}</code>，但不会生成作用域，所以如果你在对象字面量中定义属性和函数时，在这个函数里面不能直接通过属性名的方式访问定义的属性。<br>
因为对象字面量中的属性是不会纳入到对象字面量中的函数的作用域链中。<br>
如果要在对象字面量中的函数中访问对象字面量的属性，可以通过 对象字面量名.属性名的方式来使用，不过这种方式过于紧耦合，也可以通过this.属性名的方式来使用。<br>
对象函数的作用域只有当前函数内和全局。</p> <p><strong>第二种</strong><br>
使用 <code>new Object()</code>，通过对象名.属性名的方式来赋值和使用<br>
Js中所有对象底层都是关联数组。关联数组是指，下标是自定义字符串的数组。Js中没有堆、栈，js的成员几乎都是放在内存的自由存储区域中，在ES6中，也有部分成员放到静态存储区域中，比如</p> <p><img src="https://cdn.jsdelivr.net/gh/filess/img1@main/2022/07/16/1657964946112-386c3e98-e2de-4098-99d2-e42e2e35b383.png" alt=""></p> <p>为啥说js对象底层都是关联数组，原因是以下五点</p> <ol><li>数据结构类似</li> <li>都是通过[] 和 . 来访问属性</li> <li>强制赋值，都不会报错</li> <li>访问不存在的属性，都不会报错</li> <li>都可以用for in 的方式访问。</li></ol> <p><strong>第三种</strong><br>
通过定义构造函数，然后<code>new 构造函数()</code>，在构造函数中<code>{}</code>中通过this.属性名的方式赋值，在外部通过对象名.属性名的方式来使用。<br>
使用构造函数可以反复创建多个相同结构的对象。<br>
new构造函数创建对象做了4件事：</p> <ol><li>创建空对象</li> <li>调用构造函数，让新对象继承。还会自动设置新对象的__proto__指向构造函数的原型对象。</li> <li>将构造函数中的this指向新对象</li> <li>将新对象赋值给外边那个变量（如果构造函数中没有返回有效的数据，则直接返回this，这个this就是新对象，如果返回了有效数据，则直接返回有效数据给外边那个变量）。</li></ol> <h3 id="继承"><a href="#继承" class="header-anchor">#</a> 继承</h3> <p>js中继承都是通过原型对象来实现的，所有子对象共用的属性、函数就可以放到原型对象中。多个子对象都要使用相同的功能以及属性时，可以使用继承。</p> <p>原型对象是在定义构造函数的时候，默认创建的一个空对象。构造函数中都会自带一个prototype属性。</p> <p>通过对象. 的方式来访问成员时，如果在当前对象中找不到，就会在当前对象的.__proto__中去找，一层一层的网上，再找不到就会报xx不存在。</p> <p>方法或者函数中的this，一般都是谁调用，这个this就指向谁。</p> <p>例如：</p> <ol><li>obj.fn() this指向obj。</li> <li>new Fun() this指向new创建的新对象。</li> <li>原型对象中共同方法里的this会指向将来调用这个共同方法的那个子对象，例如 1 中this指向obj。</li> <li>在 <code>use strict</code> 中fn()、匿名函数自调、回调函数中的this指向undefined，这三种种情况在非严格模式下时this指向window。</li> <li>dom事件绑定函数(绑定的函数非箭头函数)时，this指向当前触发这个事件的dom对象。</li> <li>箭头函数中的this指向该函数最近的那个作用域的this。</li></ol> <p><strong>内置类型</strong><br>
每一个类型都有构造函数和原型对象<br>
内置类型有11种，ES6加了一种Symbol(唯一字符串)<br>
String， Number， Boolean，Array， Date， RegExp， Math（已经是一个对象了），Error，Function，Object，global（全局作用域对象，在浏览器中是window对象）</p> <p><strong>原型链</strong><br>
自己的__proto__指向构造函数的prototype,一层一层指向Object.prototype。<br>
现在谷歌浏览器都会将__proto__ 显示成<code>[[Prototype]]</code>，但是并不能使用，双中括号属于内部属性。</p> <h3 id="多态"><a href="#多态" class="header-anchor">#</a> 多态</h3> <p>重载：在后端中常见，允许存在相同函数名但参数签名不同的多个函数存在，也就是根据你传入的参数自动去寻找对应的某个函数，在前端中不行。<br>
重写：继承父对象的成员后，觉得不好用或者不想用，就可以在子对象中重写同名的成员，可以是属性也可以是方法。</p> <p>就像是数组的toString 和 对象的toString，它们都实现了toString的功能接口，数组的toString就是重写了对象的toString。还有Date的toString也是重写了对象的toString方法。<br>
如果你自定义的构造函数也想重写对象的toString方法，可以通过<code>构造函数.prototype.toString=函数</code>的方式来重写。</p> <h3 id="箭头函数"><a href="#箭头函数" class="header-anchor">#</a> 箭头函数</h3> <p>箭头函数就是使用bind实现的。<br>
使用时注意，被bind永久绑定的this，是不能被call再次替换。<br>
替换this的三种情况：</p> <ol><li>临时替换一次this，使用call</li> <li>临时替换一次 this, 使用apply</li> <li>永久替换 this，使用 bind</li></ol> <h3 id="创建对象的多种方式"><a href="#创建对象的多种方式" class="header-anchor">#</a> 创建对象的多种方式</h3> <p>js中创建对象很灵活，有印象了解原理即可。</p> <p>1.new Object，缺点是初始化成员的步骤过多。</p> <p>2.对象字面量创建，虽然很方便，但缺点是创建多个字面量对象时，代码会很冗余。</p> <p>3.工厂函数的方式，是对new Object的进一步的业务封装。<br>
例如：<code>function createObj(){var o = new Object(); return o;}; var obj = createObj();</code><br>
缺点是没法根据对象的原型对象来判断对象的类型。</p> <p>4.构造函数的方式，虽然可以复用代码创建多个对象，但是缺点是如果在构造函数中有成员是fn，就会导致重复创建fn，就会浪费内存。
<code>function P1(tag) { this.tag = tag; this.tip=function(){alert(this.tag)};</code></p> <p>5.原型对象的方式，定义空的构造函数，然后给构造函数的原型对象初始化成员，这样一来和new Object的缺点类似了，初始化成员步骤过多。<br> <code>function P1(tag) {}; P1.prototype.tag = tag; P1.prototype.tip=function(){alert(this.tag)</code></p> <p>6.混合模式，就是4和5的方式结合起来，可以设计的很好，也可能很糟糕，但这么做是为了规避它们各自的缺点，但还是会有缺点，是不符合面向对象封装的思想。<br>
它不是集中去的保存某类事物的数据以及功能，有点零散。<br> <code>function P1(tag) { this.tag = tag;}; P1.prototype.tip=function(){alert(this.tag)};</code></p> <p>7.动态混合，算是4和5的方式结合起来，同时也为了弥补6中缺点。
<code>function P1(tag) { this.tag = tag; if([undefined].include(P1.prototype.tip) {P1.prototype.tip=function(){alert(this.tag)}}</code><br>
这样就不零散了，只是通过构造函数创建对象时，除了第一次的if有意义，其它时候的if就多余了。</p> <p>8.寄生构造函数，构造函数A中借用构造函数B来创建对象b，然后定制化的添加对象b的成员，最后返回对象b。<br>
缺点是在复杂的情况下会导致代码可读性差。</p> <p>9.ES6的Class，这种方式比较传统，后端很多年前就有了。其实它就是4和5结合起来呀，只不过是用了<code>class {}</code>包裹4和5，然后换成后端传统的那种写法。<br> <code>class P1{ constructor(tag) {this.tag=tag;} tip() {alert(this.tag)}}</code></p> <p>10.闭包构造函数，它不用new和this了，它将2和4的方式与闭包结合起来了，非常的方便，但闭包容易内存泄露从而引发内存溢出。<br> <code>function P1(tag) {var p1= {};p.tag=tag;p1.top=function {alert(this.tag)}; return p1}; var obj = P1('hello world')</code></p> <h3 id="继承的多种方式"><a href="#继承的多种方式" class="header-anchor">#</a> 继承的多种方式</h3> <p>js中的继承也很灵活，有印象了解原理即可。</p> <p>1.原型链继承，通过原型链的方式实现继承，<code>function P1(tag) {this.tag=tag}; function P2(){};P2.prototype=new P1('hello');var p = new P2()</code></p> <p>2.构造函数继承，<code>function P1(tag) {this.tag=tag}; function P2(){P1.Call(this, 'hello');} var p = new P2();</code></p> <p>3.实例继承，构造函数A中返回构造函数B创建的对象b，同时可以在构造函数A中初始化其成员。
<code>function B() {};function A(tag){var b=new B(); b.tag=tag; return b;}; var a = new A('hello');</code></p> <p>4.拷贝继承，分为深拷贝和浅拷贝，浅拷贝只拷贝父类对象中的属性，深拷贝会递归的拷贝父类对象及以祖类对象中的属性。<br> <code>function P1(tag) {this.tag=tag}; function P2(){var p1=new P1();for(var keyName in p1 ){ P2.prototype[keyName]=p1[keyName]}}; var p = new P2();</code></p> <p>5.组合继承，是1和2结合起来，一部分通过原型继承，一部分通过构造函数继承。</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">function</span> <span class="token constant">A</span><span class="token punctuation">(</span><span class="token parameter">tag</span><span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token keyword">this</span><span class="token punctuation">.</span>tag<span class="token operator">=</span>tag<span class="token punctuation">}</span><span class="token punctuation">;</span>
<span class="token class-name">A</span><span class="token punctuation">.</span>prototype<span class="token punctuation">.</span><span class="token function-variable function">tip</span><span class="token operator">=</span><span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token function">alert</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span>tag<span class="token punctuation">)</span><span class="token punctuation">}</span><span class="token punctuation">;</span>
<span class="token keyword">function</span> <span class="token constant">B</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token constant">A</span><span class="token punctuation">.</span><span class="token function">call</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">,</span> <span class="token string">'hello'</span><span class="token punctuation">)</span><span class="token punctuation">}</span><span class="token punctuation">;</span>
<span class="token class-name">B</span><span class="token punctuation">.</span>prototype <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">A</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">B</span><span class="token punctuation">.</span>prototype<span class="token punctuation">.</span>constructor <span class="token operator">=</span> <span class="token constant">B</span><span class="token punctuation">;</span>
<span class="token keyword">var</span> b <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">B</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><p>6.寄生组合继承，是将1和2结合闭包来使用，除了像5之外，它会在闭包中对父子继承之前通过一个新的构造函数来借用父类构造函数的prototype。<br>
闭包自调的时候完成继承，借用了父类构造函数prototype。</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">function</span> <span class="token constant">A</span><span class="token punctuation">(</span><span class="token parameter">tag</span><span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token keyword">this</span><span class="token punctuation">.</span>tag<span class="token operator">=</span>tag<span class="token punctuation">}</span><span class="token punctuation">;</span>
<span class="token class-name">A</span><span class="token punctuation">.</span>prototype<span class="token punctuation">.</span><span class="token function-variable function">tip</span><span class="token operator">=</span><span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token function">alert</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span>tag<span class="token punctuation">)</span><span class="token punctuation">}</span><span class="token punctuation">;</span>
<span class="token keyword">function</span> <span class="token constant">B</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token constant">A</span><span class="token punctuation">.</span><span class="token function">call</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">,</span> <span class="token string">'hello'</span><span class="token punctuation">)</span><span class="token punctuation">}</span><span class="token punctuation">;</span>
<span class="token punctuation">(</span><span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    <span class="token keyword">var</span> <span class="token function-variable function">C</span> <span class="token operator">=</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">;</span>
    <span class="token class-name">C</span><span class="token punctuation">.</span>prototype<span class="token operator">=</span><span class="token class-name">A</span><span class="token punctuation">.</span>prototype<span class="token punctuation">;</span>
    <span class="token class-name">B</span><span class="token punctuation">.</span>prototype<span class="token operator">=</span><span class="token keyword">new</span> <span class="token class-name">C</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token keyword">var</span> b <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">B</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><p>7.ES6的Class extends继承，这种方式比较传统，原理上是5中的组合继承。<br> <code>class A { constructor(tag) {this.tag=tag;}};class B extends A {constructor() {super('hello')}}</code></p> <h3 id="对象克隆"><a href="#对象克隆" class="header-anchor">#</a> 对象克隆</h3> <p>对象克隆一般分为浅克隆和深克隆。<br>
浅克隆就是创建一个空对象，然后把另外一个要被克隆的对象遍历一遍，最后对空对象进行key/value的赋值即可。<br>
浅克隆还可以使用Object.assign(target,source)来进行，它的原理也是简单遍历一遍被克隆的对象source然后赋值给target。<br>
深克隆就是需要递归的遍历被克隆的对象，然后进行key/value的赋值，但是需要有特性的判断，比例如你遍历到数组、正则、null甚至一些特殊的对象，甚至还有边缘的处理等等。<br>
最简单的深克隆是JSON.stringify()、JSON.parse()，但无法克隆函数以及值为undefined的属性。<br>
记得似乎chrome 97 以及 node17 之后也支持了一个深克隆的api <code>structuredClone</code>，直接调用这个api即可，它是深克隆的。</p> <h2 id="es6"><a href="#es6" class="header-anchor">#</a> ES6</h2> <p>如果系统的学习ES6可以去看阮一峰老师的网站，我说的其实都是通过我学习之后理解呀总结呀觉得很常用的内容。</p> <h3 id="简单知识点"><a href="#简单知识点" class="header-anchor">#</a> 简单知识点</h3> <h4 id="模板字符串"><a href="#模板字符串" class="header-anchor">#</a> 模板字符串</h4> <p><code>${}</code><br>
它里面能放 变量、算术计算表单式、三目表达式、对象属性、创建对象、调用函数、数组元素、有返回值的合法的任意js表达式。<br>
不能放没有返回值的js表单式，什么if else for while 都不行。</p> <h4 id="let"><a href="#let" class="header-anchor">#</a> let</h4> <p>var 中的问题，声明提前(变量提升)，var 会打乱程序执行的顺序。<br>
let 很好的解决了这个问题，不会被声明提前，保证程序顺序执行。同时也让let所在的程序块形成了一个&quot;块级作用域&quot;(匿名的函数作用域)，从这个块儿内的变量不会去影响块儿外的变量。</p> <p>let 的本质：底层相当于匿名函数自调噢，也就是用匿名的函数作用域了。所以它不能像var声明变量一样可以在window上通过window.变量名的方式访问或修改。</p> <p>let 声明的变量转成匿名函数自调时是这样的，同时它不支持重复声明的。</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">let</span> a <span class="token operator">=</span> <span class="token number">2</span><span class="token punctuation">;</span>
<span class="token keyword">let</span> b <span class="token operator">=</span> <span class="token number">3</span><span class="token punctuation">;</span>

<span class="token comment">// 转换之后是这样的</span>
<span class="token punctuation">(</span><span class="token keyword">function</span><span class="token punctuation">(</span><span class="token parameter">a<span class="token punctuation">,</span> b</span><span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">)</span>
</code></pre></div><p>let 和 const原理上一样，只是const比let多两点限制。<br>
const 声明的变量必须赋初始值，并且不能直接修改这个变量的值(简单值)及引用(数组引用、对象引用)噢，但可以修改、删除、新增引用里的属性值。</p> <h4 id="再讲箭头函数"><a href="#再讲箭头函数" class="header-anchor">#</a> 再讲箭头函数</h4> <p>箭头函数就是使用bind实现的，永久绑定当前最近的那个作用域的this，并且是不能被call再次替换。</p> <p>普通函数是有arguments，但箭头函数是没有arguments的。</p> <p>最好不要在构造函数、对象方法、原型对象方法、DOM事件处理函数 这些里面使用箭头函数，会影响this的走向。<br>
箭头函数不支持new 同时 也没有 prototype属性。</p> <h4 id="for-of"><a href="#for-of" class="header-anchor">#</a> for of</h4> <p>遍历的操作很常见，比如 for 索引的方式遍历、forEeach api(数组原型、数组、伪数组)、for in、for of。</p> <p>普通的for，过于原始，索引一般的是数字类型，可以通过封装 类forEach的方法来简化。<br>
forEach api，这是基于普通的for进行封装的，经常配合箭头函数来使用，不能遍历对象。
如果是伪数组可以用Array.prototype.slice.call({0: 'a', 1: 'b', length: 2}, 0)来转换为数组，从而能够使用forEach。<br>
for in，可以用于遍历普通对象，也就是自定义下标的对象。遍历的时候可以拿到自定义下标。然后通过<code>对象名[下标]</code>的方式访问。<br>
for of，不能遍历普通对象，但是可以让它实现一个迭代器接口来支持被遍历，可以看我写的这篇文章<a href="https://juejin.cn/post/7067749950406262815" target="_blank" rel="noopener noreferrer">我的代码实现之前端迭代器<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a>。
它只能从头遍历到尾，同时只会取value，不能直接拿到下标。</p> <p>一般来说，遍历普通对象，用for in，如果是非普通对象，可以用for of，有时候forEach 和 for 索引的方式用的也很方便，看业务需求和个人风格吧。</p> <h4 id="参数与展开运算符"><a href="#参数与展开运算符" class="header-anchor">#</a> 参数与展开运算符</h4> <p>参数默认值、参数聚合(剩余参数)、参数展开。</p> <p>参数默认值就是给函数的形参赋默认值</p> <p>箭头函数中没有arguments，如果想实现arguments一样的效果可以这样<code>(...list) =&gt; {}</code>，这个list就是像argument一样简单的把参数聚合到一起了，
并且它是把参数都放到一个数组中，而不是像arguments那样是伪数组。<br>
除了全部参数聚合，还可以局部参数聚合，比如这样<code>(name, age, ...otherList) =&gt; {}</code>，这个otherList就是将前两个参数之外的参数聚合起来了。</p> <p>通过展开运算符将参数展开<br>
通过<code>...</code>可以将一个对象或者数组拆散开来从而实现浅克隆以及多合并，也可以在函数调用时候通过...展开一个数组中的参数依次赋值进去。比如~~</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">const</span> arr1 <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token keyword">const</span> arr2 <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token keyword">const</span> arr3 <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token operator">...</span>arr1<span class="token punctuation">,</span> <span class="token operator">...</span>arr2<span class="token punctuation">,</span> <span class="token number">7</span><span class="token punctuation">,</span> <span class="token number">8</span><span class="token punctuation">,</span> <span class="token number">9</span><span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token comment">// 1 2 3 4 5 6 7 8 9</span>

<span class="token keyword">const</span> obj1 <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token literal-property property">name</span><span class="token operator">:</span> <span class="token string">'m2'</span><span class="token punctuation">,</span> <span class="token literal-property property">info</span><span class="token operator">:</span> <span class="token string">'student'</span><span class="token punctuation">}</span><span class="token punctuation">;</span>
<span class="token keyword">const</span> obj2 <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token literal-property property">sex</span><span class="token operator">:</span> <span class="token string">'男'</span><span class="token punctuation">}</span><span class="token punctuation">;</span>
<span class="token keyword">const</span> obj3 <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token operator">...</span>obj1<span class="token punctuation">,</span> <span class="token operator">...</span>obj2<span class="token punctuation">,</span> <span class="token literal-property property">address</span><span class="token operator">:</span> <span class="token string">'欧罗巴'</span><span class="token punctuation">}</span><span class="token punctuation">;</span>

<span class="token keyword">function</span> <span class="token function">say</span><span class="token punctuation">(</span><span class="token parameter">info1<span class="token punctuation">,</span> info2<span class="token punctuation">,</span> info3</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token template-string"><span class="token template-punctuation string">`</span><span class="token string">1:</span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span>info1<span class="token interpolation-punctuation punctuation">}</span></span><span class="token string">, 2:</span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span>info2<span class="token interpolation-punctuation punctuation">}</span></span><span class="token string">, 3:</span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span>info3<span class="token interpolation-punctuation punctuation">}</span></span><span class="token template-punctuation string">`</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token keyword">const</span> infoList <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string">'one'</span><span class="token punctuation">,</span> <span class="token string">'two'</span><span class="token punctuation">,</span> <span class="token string">'three'</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token function">say</span><span class="token punctuation">(</span><span class="token operator">...</span>infoList<span class="token punctuation">)</span><span class="token punctuation">;</span>

Math<span class="token punctuation">.</span><span class="token function">max</span><span class="token punctuation">(</span><span class="token operator">...</span>arr3<span class="token punctuation">)</span> <span class="token comment">// 9</span>
</code></pre></div><h4 id="通过解构来提效"><a href="#通过解构来提效" class="header-anchor">#</a> 通过解构来提效</h4> <p>解构就是按需的拿到一个数据集合中的你需要的那个数据，就像sql语句按需获取部分的数据。</p> <p>解构有三种方式： 对 数组解构、对 对象解构、对函数的参数解构。</p> <p>数组解构是通过元素的顺序来解构的。<br>
对象解构是通过对象的keyName来解构的。<br>
参数解构是对入参进行解构，如果入参是数组，就像数组解构一样，如果入参是对象，就像对象解构一样。</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">// 数组解构</span>
<span class="token keyword">const</span> arr1 <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token keyword">const</span> <span class="token punctuation">[</span>one<span class="token punctuation">,</span> two<span class="token punctuation">,</span> three<span class="token punctuation">]</span> <span class="token operator">=</span> arr1<span class="token punctuation">;</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token template-string"><span class="token template-punctuation string">`</span><span class="token string">1:</span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span>one<span class="token interpolation-punctuation punctuation">}</span></span><span class="token string">, 2:</span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span>two<span class="token interpolation-punctuation punctuation">}</span></span><span class="token string">, 3:</span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span>three<span class="token interpolation-punctuation punctuation">}</span></span><span class="token template-punctuation string">`</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token keyword">const</span> arr2 <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token keyword">const</span> <span class="token punctuation">[</span><span class="token punctuation">,</span> five<span class="token punctuation">,</span> <span class="token punctuation">]</span> <span class="token operator">=</span> arr1<span class="token punctuation">;</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>five<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 5</span>

<span class="token comment">// 对象解构</span>
<span class="token keyword">const</span> obj1 <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token literal-property property">name</span><span class="token operator">:</span> <span class="token string">'m2'</span><span class="token punctuation">,</span> <span class="token literal-property property">info</span><span class="token operator">:</span> <span class="token string">'student'</span><span class="token punctuation">,</span> <span class="token literal-property property">sex</span><span class="token operator">:</span> <span class="token string">'男'</span><span class="token punctuation">}</span><span class="token punctuation">;</span>
<span class="token keyword">const</span> <span class="token punctuation">{</span> name<span class="token punctuation">,</span> info <span class="token punctuation">}</span> <span class="token operator">=</span> obj1<span class="token punctuation">;</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token template-string"><span class="token template-punctuation string">`</span><span class="token string">name:</span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span>name<span class="token interpolation-punctuation punctuation">}</span></span><span class="token string">, info:</span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span>info<span class="token interpolation-punctuation punctuation">}</span></span><span class="token template-punctuation string">`</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment">// 参数解构</span>
<span class="token keyword">const</span> arr2 <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token keyword">function</span> <span class="token function">say</span><span class="token punctuation">(</span><span class="token parameter"><span class="token punctuation">[</span>info1<span class="token punctuation">,</span> info2<span class="token punctuation">,</span> info3<span class="token punctuation">]</span></span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token template-string"><span class="token template-punctuation string">`</span><span class="token string">1:</span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span>info1<span class="token interpolation-punctuation punctuation">}</span></span><span class="token string">, 2:</span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span>info2<span class="token interpolation-punctuation punctuation">}</span></span><span class="token string">, 3:</span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span>info3<span class="token interpolation-punctuation punctuation">}</span></span><span class="token template-punctuation string">`</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token function">say</span><span class="token punctuation">(</span>arr2<span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token keyword">const</span> obj2 <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token literal-property property">name</span><span class="token operator">:</span> <span class="token string">'m2'</span><span class="token punctuation">,</span> <span class="token literal-property property">info</span><span class="token operator">:</span> <span class="token string">'student'</span><span class="token punctuation">,</span> <span class="token literal-property property">sex</span><span class="token operator">:</span> <span class="token string">'男'</span><span class="token punctuation">,</span> <span class="token literal-property property">address</span><span class="token operator">:</span> <span class="token string">'欧罗巴'</span><span class="token punctuation">}</span><span class="token punctuation">;</span>
<span class="token keyword">function</span> <span class="token function">say2</span><span class="token punctuation">(</span><span class="token parameter"><span class="token punctuation">{</span> name<span class="token punctuation">,</span> info<span class="token punctuation">,</span> sex<span class="token punctuation">}</span></span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token template-string"><span class="token template-punctuation string">`</span><span class="token string">1:</span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span>info1<span class="token interpolation-punctuation punctuation">}</span></span><span class="token string">, 2:</span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span>info2<span class="token interpolation-punctuation punctuation">}</span></span><span class="token string">, 3:</span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span>info3<span class="token interpolation-punctuation punctuation">}</span></span><span class="token template-punctuation string">`</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token function">say2</span><span class="token punctuation">(</span>obj2<span class="token punctuation">)</span>
</code></pre></div><p>解构失败不报错，会给变量赋值为undefined。</p> <h3 id="class-与-它的继承原理"><a href="#class-与-它的继承原理" class="header-anchor">#</a> class 与 它的继承原理</h3> <p>class中的定义的函数会放到原型对象上，但是constractor中赋值的属性和class {}中普通定义的属性都会放到new 之后的实例对象上。</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">class</span> <span class="token class-name">M2</span> <span class="token punctuation">{</span>
  name<span class="token operator">=</span><span class="token string">'m2'</span>

  <span class="token function">constractor</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span>age <span class="token operator">=</span> <span class="token number">20</span>
  <span class="token punctuation">}</span>

  <span class="token function">say</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token template-string"><span class="token template-punctuation string">`</span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span><span class="token keyword">this</span><span class="token punctuation">.</span>name<span class="token interpolation-punctuation punctuation">}</span></span><span class="token string"> </span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span><span class="token keyword">this</span><span class="token punctuation">.</span>age<span class="token interpolation-punctuation punctuation">}</span></span><span class="token template-punctuation string">`</span></span><span class="token punctuation">)</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre></div><p>多个实例对象共用的属性，不再是放到原型对象上，而是作为class 的静态属性来存放，直接通过class名.属性的方式。定义时是通过 static来修饰的。他并没存放到原型对象去，而是直接放到自己身上，所以才能通过类名.xx的方式使用。</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">class</span> <span class="token class-name">M2</span> <span class="token punctuation">{</span>
  <span class="token keyword">static</span> name<span class="token operator">=</span><span class="token string">'m2'</span>
<span class="token punctuation">}</span>
</code></pre></div><h4 id="继承-extends-和-调用父类-构造函数-super"><a href="#继承-extends-和-调用父类-构造函数-super" class="header-anchor">#</a> 继承 extends 和 调用父类 构造函数 super</h4> <p>继承是通过extends关键字来进行的，如果需要调用父类构造函数来初始化成员，也可以使用super来调。</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">class</span> <span class="token class-name">Parent</span> <span class="token punctuation">{</span>
  <span class="token function">constractor</span> <span class="token punctuation">(</span><span class="token parameter">name</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span>name <span class="token operator">=</span> name
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>

<span class="token keyword">class</span> <span class="token class-name">M2</span> <span class="token keyword">extends</span> <span class="token class-name">Parent</span> <span class="token punctuation">{</span>
  <span class="token function">constractor</span> <span class="token punctuation">(</span><span class="token parameter">name<span class="token punctuation">,</span> age</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">super</span><span class="token punctuation">(</span>name<span class="token punctuation">)</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span>age <span class="token operator">=</span> age
  <span class="token punctuation">}</span>

  <span class="token function">parint</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token function">consolog</span><span class="token punctuation">(</span><span class="token template-string"><span class="token template-punctuation string">`</span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span><span class="token keyword">this</span><span class="token punctuation">.</span>name<span class="token interpolation-punctuation punctuation">}</span></span><span class="token string"> </span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span><span class="token keyword">this</span><span class="token punctuation">.</span>age<span class="token interpolation-punctuation punctuation">}</span></span><span class="token string">岁(狗头)</span><span class="token template-punctuation string">`</span></span><span class="token punctuation">)</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>

<span class="token keyword">const</span> m2 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">M2</span> <span class="token punctuation">(</span><span class="token string">'码二'</span><span class="token punctuation">,</span> <span class="token string">'1000'</span><span class="token punctuation">)</span>
m2<span class="token punctuation">.</span><span class="token function">parint</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 码二 1000岁(狗头)</span>
</code></pre></div><p>继承是为了更方便的扩展，但是可能会造成子类职责变重，因为一下子就把父类的功能全弄过来了，有时并不需要那么多，那么可以通过组合或聚合的方式来按需拿自己需要的功能。</p> <p><strong>面向对象小知识点</strong><br>
先有子类型，再通过子类型抽象出父类型</p> <h3 id="promise-以及-它的扩展"><a href="#promise-以及-它的扩展" class="header-anchor">#</a> Promise 以及 它的扩展</h3> <p>由于js单线程的特性，所以就有了异步函数，由于异步函数之间互不干扰，如果想要让一个异步函数的执行结果传给另一个异步函数，那么就需要不停的做函数嵌套。这样的套娃使得代码比较难看也不宜维护。于是Promise横空出世了。<br>
promise 用来解决这种函数嵌套与代码难看的“回调地狱”问题。</p> <p>使用promise 会优先将要调用的函数进行收集，收集完毕后，再依次执行，然后执行的结果会按需的有序的传递下去。</p> <p>为了更准确的控制promise收集的这些函数的执行过程，所以它用到了三个状态<br>
pending 任务挂起<br>
fulfilled 执行成功<br>
rejected  执行出错</p> <div class="language-js extra-class"><pre class="language-js"><code>Promise<span class="token punctuation">.</span><span class="token function">resolve</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">then</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">catch</span><span class="token punctuation">(</span><span class="token punctuation">)</span>

<span class="token keyword">new</span> <span class="token class-name">Promise</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token parameter">resolve<span class="token punctuation">,</span> rejected</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
  <span class="token function">setTimeout</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>Math<span class="token punctuation">.</span><span class="token function">random</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">&gt;</span> <span class="token number">0.5</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
      <span class="token function">resolve</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
    <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
      <span class="token function">rejected</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
    <span class="token punctuation">}</span>
  <span class="token punctuation">}</span><span class="token punctuation">,</span><span class="token number">2000</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">then</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">catch</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
</code></pre></div><h4 id="async-和-await-简化promise"><a href="#async-和-await-简化promise" class="header-anchor">#</a> async 和 await 简化promise</h4> <p>它们基于promise，是promise的语法糖，它使用generator生成器来实现的。<br>
主要功能是为了消除嵌套，虽然promise将函数的嵌套降到了几乎仅嵌套一层的地步，但是似乎还可以再优化，所以async 和 await 出现了，使得把代码变成了同步的写法。<br>
其实还是收集异步函数，然后依次执行的底层逻辑。</p> <div class="language-js extra-class"><pre class="language-js"><code>Promise<span class="token punctuation">.</span><span class="token function">resolve</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">.</span><span class="token function">then</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token function">setTimeout</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">'action 1'</span><span class="token punctuation">)</span><span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token number">500</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token punctuation">.</span><span class="token function">then</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token function">setTimeout</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">'action 2'</span><span class="token punctuation">)</span><span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token number">3800</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token punctuation">.</span><span class="token function">catch</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token parameter">e</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">'action error'</span><span class="token punctuation">,</span> e<span class="token punctuation">)</span><span class="token punctuation">)</span>

<span class="token comment">// 将promise的写法换成 async await</span>
<span class="token punctuation">(</span><span class="token keyword">async</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
  <span class="token keyword">try</span> <span class="token punctuation">{</span>
    <span class="token keyword">await</span> <span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">=&gt;</span> <span class="token function">setTimeout</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">'action 1'</span><span class="token punctuation">)</span><span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token number">500</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
    <span class="token keyword">await</span> <span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">=&gt;</span> <span class="token function">setTimeout</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">'action 2'</span><span class="token punctuation">)</span><span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token number">3800</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
  <span class="token punctuation">}</span> <span class="token keyword">catch</span><span class="token punctuation">(</span>e<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">'action error'</span><span class="token punctuation">,</span> e<span class="token punctuation">)</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
</code></pre></div><h3 id="promise-状态机"><a href="#promise-状态机" class="header-anchor">#</a> Promise 状态机</h3> <p>之所以Promise能够实现这个效果这样的一个状态管理机制。<br>
不过这样的一个状态机制也比较常见。<br>
那么可以简单的实现一下promise，从中体会其中的大概原理。</p> <h4 id="_1-状态机"><a href="#_1-状态机" class="header-anchor">#</a> 1.状态机</h4> <p>Promise中保存了state 和 value 以及 then里的回调。</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">const</span> <span class="token constant">PENDING</span> <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> <span class="token comment">// 挂起的状态</span>
<span class="token keyword">const</span> <span class="token constant">FULFILLED</span> <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span> <span class="token comment">// 执行成功的状态</span>
<span class="token keyword">const</span> <span class="token constant">REJECTED</span> <span class="token operator">=</span> <span class="token number">2</span><span class="token punctuation">;</span> <span class="token comment">// 执行失败的状态</span>

<span class="token comment">// 定义一个Promise函数，以他为构造函数，用来创建promise对象</span>
<span class="token keyword">function</span> <span class="token function">Promise</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>

    <span class="token keyword">let</span> state <span class="token operator">=</span> <span class="token constant">PENDING</span><span class="token punctuation">;</span> <span class="token comment">// 存储当前promise的状态</span>

    <span class="token keyword">let</span> value <span class="token operator">=</span> <span class="token keyword">null</span><span class="token punctuation">;</span> <span class="token comment">// 存储执行成功后的返回值 or 执行失败后的异常信息</span>

    <span class="token keyword">const</span> handlers <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token comment">// 存储promise.then时传入的每一个回调函数(任务)</span>
<span class="token punctuation">}</span>
</code></pre></div><h4 id="_2-参与状态变迁的两个底层函数"><a href="#_2-参与状态变迁的两个底层函数" class="header-anchor">#</a> 2.参与状态变迁的两个底层函数</h4> <p>这两个底层函数是fulfill和reject</p> <div class="language-js extra-class"><pre class="language-js"><code>
<span class="token keyword">const</span> <span class="token constant">PENDING</span> <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> <span class="token comment">// 挂起的状态</span>
<span class="token keyword">const</span> <span class="token constant">FULFILLED</span> <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span> <span class="token comment">// 执行成功的状态</span>
<span class="token keyword">const</span> <span class="token constant">REJECTED</span> <span class="token operator">=</span> <span class="token number">2</span><span class="token punctuation">;</span> <span class="token comment">// 执行失败的状态</span>

<span class="token comment">// 定义一个Promise函数，以他为构造函数，用来创建promise对象</span>
<span class="token keyword">function</span> <span class="token function">Promise</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>

    <span class="token keyword">let</span> state <span class="token operator">=</span> <span class="token constant">PENDING</span><span class="token punctuation">;</span> <span class="token comment">// 存储当前promise的状态</span>

    <span class="token keyword">let</span> value <span class="token operator">=</span> <span class="token keyword">null</span><span class="token punctuation">;</span> <span class="token comment">// 存储执行成功后的返回值 or 执行失败后的异常信息</span>

    <span class="token keyword">const</span> handlers <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token comment">// 存储promise.then时传入的每一个回调函数(任务)</span>

    <span class="token comment">// 执行成功后，将状态变更为执行成功的状态，同时将执行结果返回值进行保存，存到value变量中</span>
    <span class="token keyword">function</span> <span class="token function">fulfill</span> <span class="token punctuation">(</span><span class="token parameter">result</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        state <span class="token operator">=</span> <span class="token constant">FULFILLED</span>
        value <span class="token operator">=</span> result
    <span class="token punctuation">}</span>

    <span class="token comment">// 执行失败后，将状态变更为执行失败的状态，同时将执行结果返回值进行保存，存到value变量中</span>
    <span class="token keyword">function</span> <span class="token function">reject</span> <span class="token punctuation">(</span><span class="token parameter">error</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        state <span class="token operator">=</span> <span class="token constant">REJECTED</span>
        value <span class="token operator">=</span> error
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>


</code></pre></div><h4 id="_3-对外开放一个resolve方法"><a href="#_3-对外开放一个resolve方法" class="header-anchor">#</a> 3.对外开放一个resolve方法</h4> <div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">// 定义一个Promise函数，以他为构造函数，用来创建promise对象</span>
<span class="token keyword">function</span> <span class="token function">Promise</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>

    <span class="token keyword">let</span> state <span class="token operator">=</span> <span class="token constant">PENDING</span><span class="token punctuation">;</span> <span class="token comment">// 存储当前promise的状态</span>

    <span class="token keyword">let</span> value <span class="token operator">=</span> <span class="token keyword">null</span><span class="token punctuation">;</span> <span class="token comment">// 存储执行成功后的返回值 or 执行失败后的异常信息</span>

    <span class="token keyword">const</span> handlers <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token comment">// 存储promise.then时传入的每一个回调函数(任务)</span>

    <span class="token comment">// 执行成功后，将状态变更为执行成功的状态，同时将执行结果返回值进行保存，存到value变量中</span>
    <span class="token keyword">function</span> <span class="token function">fulfill</span> <span class="token punctuation">(</span><span class="token parameter">result</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        state <span class="token operator">=</span> <span class="token constant">FULFILLED</span>
        value <span class="token operator">=</span> result
    <span class="token punctuation">}</span>

    <span class="token comment">// 执行失败后，将状态变更为执行失败的状态，同时将执行结果返回值进行保存，存到value变量中</span>
    <span class="token keyword">function</span> <span class="token function">reject</span> <span class="token punctuation">(</span><span class="token parameter">error</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        state <span class="token operator">=</span> <span class="token constant">REJECTED</span>
        value <span class="token operator">=</span> error
    <span class="token punctuation">}</span>

    <span class="token comment">// 如果当前任务成功执行了，那么使用者就会去调用resolve(返回值)</span>
    <span class="token keyword">function</span> <span class="token function">resolve</span><span class="token punctuation">(</span><span class="token parameter">result</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">try</span> <span class="token punctuation">{</span>
            <span class="token keyword">let</span> then <span class="token operator">=</span> <span class="token function">getThen</span><span class="token punctuation">(</span>result<span class="token punctuation">)</span> <span class="token comment">// 拿到任务返回值中的新任务(回调函数) 或者 新的promise对象，这是因为.then(()=&gt;XX)中的函数执行时，可能会返回一个新函数或者promise对象</span>
            <span class="token comment">// 如果有拿到新任务或者新任务对象</span>
            <span class="token keyword">if</span> <span class="token punctuation">(</span>then<span class="token punctuation">)</span> <span class="token punctuation">{</span>
                <span class="token comment">// 执行新的任务函数，并把任务对象绑定到这个函数上，与此同时将resolve 和 reject 作为参数传递给这个新任务函数</span>
                <span class="token function">doResolve</span><span class="token punctuation">(</span><span class="token function">then</span><span class="token punctuation">.</span><span class="token function">bind</span><span class="token punctuation">(</span>result<span class="token punctuation">)</span><span class="token punctuation">,</span> resolve<span class="token punctuation">,</span> reject<span class="token punctuation">)</span>
                <span class="token keyword">return</span> <span class="token comment">// 结束</span>
            <span class="token punctuation">}</span>

            <span class="token comment">// 如果没有新的任务，那就直接成功</span>
            <span class="token function">fulfill</span><span class="token punctuation">(</span>result<span class="token punctuation">)</span>
        <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span>error<span class="token punctuation">)</span> <span class="token punctuation">{</span>
           <span class="token comment">// 执行过程中报错了，那就直接失败</span>
           <span class="token function">reject</span><span class="token punctuation">(</span>error<span class="token punctuation">)</span> 
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre></div><h4 id="_4-实现getthen和doresolve函数"><a href="#_4-实现getthen和doresolve函数" class="header-anchor">#</a> 4.实现getThen和doResolve函数</h4> <p>获取then中返回的新promise对象的then，如果有就会执行then</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">// 定义一个Promise函数，以他为构造函数，用来创建promise对象</span>
<span class="token keyword">function</span> <span class="token function">Promise</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>

    <span class="token keyword">let</span> state <span class="token operator">=</span> <span class="token constant">PENDING</span><span class="token punctuation">;</span> <span class="token comment">// 存储当前promise的状态</span>

    <span class="token keyword">let</span> value <span class="token operator">=</span> <span class="token keyword">null</span><span class="token punctuation">;</span> <span class="token comment">// 存储执行成功后的返回值 or 执行失败后的异常信息</span>

    <span class="token keyword">const</span> handlers <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token comment">// 存储promise.then时传入的每一个回调函数(任务)</span>

    <span class="token comment">// 执行成功后，将状态变更为执行成功的状态，同时将执行结果返回值进行保存，存到value变量中</span>
    <span class="token keyword">function</span> <span class="token function">fulfill</span> <span class="token punctuation">(</span><span class="token parameter">result</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        state <span class="token operator">=</span> <span class="token constant">FULFILLED</span>
        value <span class="token operator">=</span> result
    <span class="token punctuation">}</span>

    <span class="token comment">// 执行失败后，将状态变更为执行失败的状态，同时将执行结果返回值进行保存，存到value变量中</span>
    <span class="token keyword">function</span> <span class="token function">reject</span> <span class="token punctuation">(</span><span class="token parameter">error</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        state <span class="token operator">=</span> <span class="token constant">REJECTED</span>
        value <span class="token operator">=</span> error
    <span class="token punctuation">}</span>

    <span class="token comment">// 如果当前任务成功执行了，那么使用者就会去调用resolve(返回值)</span>
    <span class="token keyword">function</span> <span class="token function">resolve</span><span class="token punctuation">(</span><span class="token parameter">result</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">try</span> <span class="token punctuation">{</span>
            <span class="token keyword">let</span> then <span class="token operator">=</span> <span class="token function">getThen</span><span class="token punctuation">(</span>result<span class="token punctuation">)</span> <span class="token comment">// 拿到任务返回值中新的promise对象的then(新任务)，这是因为之前的.then(()=&gt;XX)中的函数执行时，可能会返回一个新promise对象</span>
            <span class="token comment">// 如果有拿到新任务</span>
            <span class="token keyword">if</span> <span class="token punctuation">(</span>then<span class="token punctuation">)</span> <span class="token punctuation">{</span>
                <span class="token comment">// 执行新的任务函数，并把任务对象绑定到这个函数上，与此同时将resolve 和 reject 作为参数传递给这个新任务函数</span>
                <span class="token function">doResolve</span><span class="token punctuation">(</span><span class="token function">then</span><span class="token punctuation">.</span><span class="token function">bind</span><span class="token punctuation">(</span>result<span class="token punctuation">)</span><span class="token punctuation">,</span> resolve<span class="token punctuation">,</span> reject<span class="token punctuation">)</span>
                <span class="token keyword">return</span> <span class="token comment">// 结束</span>
            <span class="token punctuation">}</span>

            <span class="token comment">// 如果没有新的任务或者只是返回一个普通的函数，那就让当前的任务直接成功即可。</span>
            <span class="token function">fulfill</span><span class="token punctuation">(</span>result<span class="token punctuation">)</span>
        <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span>error<span class="token punctuation">)</span> <span class="token punctuation">{</span>
           <span class="token comment">// 执行过程中报错了，那就直接失败</span>
           <span class="token function">reject</span><span class="token punctuation">(</span>error<span class="token punctuation">)</span> 
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>

    <span class="token comment">// 拿到新的promise对象中的then</span>
    <span class="token keyword">function</span> <span class="token function">getThen</span> <span class="token punctuation">(</span><span class="token parameter">value</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">const</span> t <span class="token operator">=</span> <span class="token keyword">typeof</span> value
        <span class="token keyword">if</span> <span class="token punctuation">(</span>value <span class="token operator">&amp;&amp;</span> <span class="token punctuation">[</span><span class="token string">'object'</span><span class="token punctuation">,</span> <span class="token string">'function'</span><span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">includes</span><span class="token punctuation">(</span>t<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token comment">// 如果value 是一个promise对象，那就返回这个promise对象的then</span>
            <span class="token keyword">const</span> then <span class="token operator">=</span> value<span class="token punctuation">.</span>then
            <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token string">'function'</span> <span class="token operator">===</span> <span class="token keyword">typeof</span> then<span class="token punctuation">)</span> <span class="token punctuation">{</span>
                <span class="token keyword">return</span> then
            <span class="token punctuation">}</span>
            <span class="token comment">// 如果value是一个普通的函数，走下面的null逻辑，因为普通函数不用对状态进行干预了。</span>
        <span class="token punctuation">}</span>
        <span class="token comment">// value 为空，就返回null</span>
        <span class="token keyword">return</span> <span class="token keyword">null</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token comment">// 执行任务函数promise的then函数</span>
    <span class="token keyword">function</span> <span class="token function">doResolve</span> <span class="token punctuation">(</span><span class="token parameter">fn<span class="token punctuation">,</span> onFulfilled<span class="token punctuation">,</span> onRejected</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">let</span> done <span class="token operator">=</span> <span class="token boolean">false</span> <span class="token comment">// 默认状态为暂未执行成功，也是加个锁</span>
        <span class="token keyword">try</span> <span class="token punctuation">{</span>
            <span class="token comment">// 调用任务函数</span>
            <span class="token function">fn</span><span class="token punctuation">(</span>
                <span class="token comment">// 第一个回调</span>
                <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token parameter">value</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
                    <span class="token keyword">if</span><span class="token punctuation">(</span>done<span class="token punctuation">)</span> <span class="token keyword">return</span><span class="token punctuation">;</span> <span class="token comment">// 异步任务执行完毕，就没必要往下走了。因为任务函数中的两个函数有一个已经执行完了</span>
                    done <span class="token operator">=</span> <span class="token boolean">true</span><span class="token punctuation">;</span>
                    <span class="token function">onFulfilled</span><span class="token punctuation">(</span>value<span class="token punctuation">)</span>
                <span class="token punctuation">}</span><span class="token punctuation">,</span> 
                <span class="token comment">// 第二个回调</span>
                <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token parameter">reason</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
                    <span class="token keyword">if</span> <span class="token punctuation">(</span>done<span class="token punctuation">)</span> <span class="token keyword">return</span><span class="token punctuation">;</span> <span class="token comment">// 异步任务执行完毕，就没必要往下走了。因为任务函数中的两个函数有一个已经执行完了</span>
                    done <span class="token operator">=</span> <span class="token boolean">true</span><span class="token punctuation">;</span>
                    <span class="token function">onRejected</span><span class="token punctuation">(</span>reason<span class="token punctuation">)</span>
                <span class="token punctuation">}</span>
            <span class="token punctuation">)</span>
        <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span>error<span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token keyword">if</span><span class="token punctuation">(</span>done<span class="token punctuation">)</span> <span class="token keyword">return</span>
            done <span class="token operator">=</span> <span class="token boolean">true</span>
            <span class="token function">onRejected</span><span class="token punctuation">(</span>error<span class="token punctuation">)</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre></div><h4 id="_5-定义handle函数结合handlers来根据promise对象的状态来进行处理任务"><a href="#_5-定义handle函数结合handlers来根据promise对象的状态来进行处理任务" class="header-anchor">#</a> 5.定义handle函数结合handlers来根据promise对象的状态来进行处理任务</h4> <p>根据promise的运行状态来决定是收集then中的回调，还是直接执行then中的回调(这个回调其实做了一层封装，是一个对象，对象中有两个函数，成功和失败的回调)</p> <div class="language-js extra-class"><pre class="language-js"><code>
<span class="token comment">// 定义一个Promise函数，以他为构造函数，用来创建promise对象</span>
<span class="token keyword">function</span> <span class="token function">Promise</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>

    <span class="token keyword">let</span> state <span class="token operator">=</span> <span class="token constant">PENDING</span><span class="token punctuation">;</span> <span class="token comment">// 存储当前promise的状态</span>

    <span class="token keyword">let</span> value <span class="token operator">=</span> <span class="token keyword">null</span><span class="token punctuation">;</span> <span class="token comment">// 存储执行成功后的返回值 or 执行失败后的异常信息</span>

    <span class="token keyword">const</span> handlers <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token comment">// 存储promise.then时传入的每一个回调函数(任务)</span>

    <span class="token comment">// 执行成功后，将状态变更为执行成功的状态，同时将执行结果返回值进行保存，存到value变量中</span>
    <span class="token keyword">function</span> <span class="token function">fulfill</span> <span class="token punctuation">(</span><span class="token parameter">result</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        state <span class="token operator">=</span> <span class="token constant">FULFILLED</span>
        value <span class="token operator">=</span> result
    <span class="token punctuation">}</span>

    <span class="token comment">// 执行失败后，将状态变更为执行失败的状态，同时将执行结果返回值进行保存，存到value变量中</span>
    <span class="token keyword">function</span> <span class="token function">reject</span> <span class="token punctuation">(</span><span class="token parameter">error</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        state <span class="token operator">=</span> <span class="token constant">REJECTED</span>
        value <span class="token operator">=</span> error
    <span class="token punctuation">}</span>

    <span class="token comment">// 如果当前任务成功执行了，那么使用者就会去调用resolve(返回值)</span>
    <span class="token keyword">function</span> <span class="token function">resolve</span><span class="token punctuation">(</span><span class="token parameter">result</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">try</span> <span class="token punctuation">{</span>
            <span class="token keyword">let</span> then <span class="token operator">=</span> <span class="token function">getThen</span><span class="token punctuation">(</span>result<span class="token punctuation">)</span> <span class="token comment">// 拿到任务返回值中新的promise对象的then(新任务)，这是因为之前的.then(()=&gt;XX)中的函数执行时，可能会返回一个新promise对象</span>
            <span class="token comment">// 如果有拿到新任务</span>
            <span class="token keyword">if</span> <span class="token punctuation">(</span>then<span class="token punctuation">)</span> <span class="token punctuation">{</span>
                <span class="token comment">// 执行新的任务函数，并把任务对象绑定到这个函数上，与此同时将resolve 和 reject 作为参数传递给这个新任务函数</span>
                <span class="token function">doResolve</span><span class="token punctuation">(</span><span class="token function">then</span><span class="token punctuation">.</span><span class="token function">bind</span><span class="token punctuation">(</span>result<span class="token punctuation">)</span><span class="token punctuation">,</span> resolve<span class="token punctuation">,</span> reject<span class="token punctuation">)</span>
                <span class="token keyword">return</span> <span class="token comment">// 结束</span>
            <span class="token punctuation">}</span>

            <span class="token comment">// 如果没有新的任务或者只是返回一个普通的函数，那就让当前的任务直接成功即可。</span>
            <span class="token function">fulfill</span><span class="token punctuation">(</span>result<span class="token punctuation">)</span>
        <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span>error<span class="token punctuation">)</span> <span class="token punctuation">{</span>
           <span class="token comment">// 执行过程中报错了，那就直接失败</span>
           <span class="token function">reject</span><span class="token punctuation">(</span>error<span class="token punctuation">)</span> 
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>

    <span class="token comment">// 拿到新的promise对象中的then</span>
    <span class="token keyword">function</span> <span class="token function">getThen</span> <span class="token punctuation">(</span><span class="token parameter">value</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">const</span> t <span class="token operator">=</span> <span class="token keyword">typeof</span> value
        <span class="token keyword">if</span> <span class="token punctuation">(</span>value <span class="token operator">&amp;&amp;</span> <span class="token punctuation">[</span><span class="token string">'object'</span><span class="token punctuation">,</span> <span class="token string">'function'</span><span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">includes</span><span class="token punctuation">(</span>t<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token comment">// 如果value 是一个promise对象，那就返回这个promise对象的then</span>
            <span class="token keyword">const</span> then <span class="token operator">=</span> value<span class="token punctuation">.</span>then
            <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token string">'function'</span> <span class="token operator">===</span> <span class="token keyword">typeof</span> then<span class="token punctuation">)</span> <span class="token punctuation">{</span>
                <span class="token keyword">return</span> then
            <span class="token punctuation">}</span>
            <span class="token comment">// 如果value是一个普通的函数，走下面的null逻辑，因为普通函数不用对状态进行干预了。</span>
        <span class="token punctuation">}</span>
        <span class="token comment">// value 为空，就返回null</span>
        <span class="token keyword">return</span> <span class="token keyword">null</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token comment">// 执行任务函数promise的then函数</span>
    <span class="token keyword">function</span> <span class="token function">doResolve</span> <span class="token punctuation">(</span><span class="token parameter">fn<span class="token punctuation">,</span> onFulfilled<span class="token punctuation">,</span> onRejected</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">let</span> done <span class="token operator">=</span> <span class="token boolean">false</span> <span class="token comment">// 默认状态为暂未执行成功，也是加个锁</span>
        <span class="token keyword">try</span> <span class="token punctuation">{</span>
            <span class="token comment">// 调用任务函数</span>
            <span class="token function">fn</span><span class="token punctuation">(</span>
                <span class="token comment">// 第一个回调</span>
                <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token parameter">value</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
                    <span class="token keyword">if</span><span class="token punctuation">(</span>done<span class="token punctuation">)</span> <span class="token keyword">return</span><span class="token punctuation">;</span> <span class="token comment">// 异步任务执行完毕，就没必要往下走了。因为任务函数中的两个函数有一个已经执行完了</span>
                    done <span class="token operator">=</span> <span class="token boolean">true</span><span class="token punctuation">;</span>
                    <span class="token function">onFulfilled</span><span class="token punctuation">(</span>value<span class="token punctuation">)</span>
                <span class="token punctuation">}</span><span class="token punctuation">,</span> 
                <span class="token comment">// 第二个回调</span>
                <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token parameter">reason</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
                    <span class="token keyword">if</span> <span class="token punctuation">(</span>done<span class="token punctuation">)</span> <span class="token keyword">return</span><span class="token punctuation">;</span> <span class="token comment">// 异步任务执行完毕，就没必要往下走了。因为任务函数中的两个函数有一个已经执行完了</span>
                    done <span class="token operator">=</span> <span class="token boolean">true</span><span class="token punctuation">;</span>
                    <span class="token function">onRejected</span><span class="token punctuation">(</span>reason<span class="token punctuation">)</span>
                <span class="token punctuation">}</span>
            <span class="token punctuation">)</span>
        <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span>error<span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token keyword">if</span><span class="token punctuation">(</span>done<span class="token punctuation">)</span> <span class="token keyword">return</span>
            done <span class="token operator">=</span> <span class="token boolean">true</span>
            <span class="token function">onRejected</span><span class="token punctuation">(</span>error<span class="token punctuation">)</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>

    <span class="token comment">// 向外暴露的then收集的任务都会被二次封装，才会放入handlers或者直接执行掉</span>
    <span class="token keyword">function</span> <span class="token function">handle</span> <span class="token punctuation">(</span><span class="token parameter">handler</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span>state <span class="token operator">===</span> <span class="token constant">PENDING</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            handlers<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>handler<span class="token punctuation">)</span>
            <span class="token keyword">return</span>
        <span class="token punctuation">}</span>

        <span class="token keyword">if</span> <span class="token punctuation">(</span>state <span class="token operator">===</span> <span class="token constant">FULFILLED</span> <span class="token operator">&amp;&amp;</span> <span class="token string">'function'</span> <span class="token operator">===</span> <span class="token keyword">typeof</span> handler<span class="token punctuation">.</span>onFulfilled<span class="token punctuation">)</span> <span class="token punctuation">{</span>
            handler<span class="token punctuation">.</span><span class="token function">onFulfilled</span><span class="token punctuation">(</span>value<span class="token punctuation">)</span>
        <span class="token punctuation">}</span>

        <span class="token keyword">if</span> <span class="token punctuation">(</span>state <span class="token operator">===</span> <span class="token constant">REJECTED</span> <span class="token operator">&amp;&amp;</span> <span class="token string">'function'</span> <span class="token operator">===</span> <span class="token keyword">typeof</span> handler<span class="token punctuation">.</span>onRejected<span class="token punctuation">)</span> <span class="token punctuation">{</span>
            handler<span class="token punctuation">.</span><span class="token function">onRejected</span><span class="token punctuation">(</span>value<span class="token punctuation">)</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre></div><h4 id="_6-实现done并且向外暴露then"><a href="#_6-实现done并且向外暴露then" class="header-anchor">#</a> 6.实现done并且向外暴露then</h4> <p>异步的执行handle，其实每一个then都会返回新的promise对象，会根据前一个promise状态来对then进行不同的处理。如果状态未pending，就收集then中的回调，如果状态为 成功或者失败，那么就直接执行then中的回调。</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">// 定义一个Promise函数，以他为构造函数，用来创建promise对象</span>
<span class="token keyword">function</span> <span class="token function">Promise</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>

    <span class="token keyword">let</span> state <span class="token operator">=</span> <span class="token constant">PENDING</span><span class="token punctuation">;</span> <span class="token comment">// 存储当前promise的状态</span>

    <span class="token keyword">let</span> value <span class="token operator">=</span> <span class="token keyword">null</span><span class="token punctuation">;</span> <span class="token comment">// 存储执行成功后的返回值 or 执行失败后的异常信息</span>

    <span class="token keyword">const</span> handlers <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token comment">// 存储promise.then时传入的每一个回调函数(任务)</span>

    <span class="token comment">// 执行成功后，将状态变更为执行成功的状态，同时将执行结果返回值进行保存，存到value变量中</span>
    <span class="token keyword">function</span> <span class="token function">fulfill</span> <span class="token punctuation">(</span><span class="token parameter">result</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        state <span class="token operator">=</span> <span class="token constant">FULFILLED</span>
        value <span class="token operator">=</span> result
    <span class="token punctuation">}</span>

    <span class="token comment">// 执行失败后，将状态变更为执行失败的状态，同时将执行结果返回值进行保存，存到value变量中</span>
    <span class="token keyword">function</span> <span class="token function">reject</span> <span class="token punctuation">(</span><span class="token parameter">error</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        state <span class="token operator">=</span> <span class="token constant">REJECTED</span>
        value <span class="token operator">=</span> error
    <span class="token punctuation">}</span>

    <span class="token comment">// 如果当前任务成功执行了，那么使用者就会去调用resolve(返回值)</span>
    <span class="token keyword">function</span> <span class="token function">resolve</span><span class="token punctuation">(</span><span class="token parameter">result</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">try</span> <span class="token punctuation">{</span>
            <span class="token keyword">let</span> then <span class="token operator">=</span> <span class="token function">getThen</span><span class="token punctuation">(</span>result<span class="token punctuation">)</span> <span class="token comment">// 拿到任务返回值中新的promise对象的then(新任务)，这是因为之前的.then(()=&gt;XX)中的函数执行时，可能会返回一个新promise对象</span>
            <span class="token comment">// 如果有拿到新任务</span>
            <span class="token keyword">if</span> <span class="token punctuation">(</span>then<span class="token punctuation">)</span> <span class="token punctuation">{</span>
                <span class="token comment">// 执行新的任务函数，并把任务对象绑定到这个函数上，与此同时将resolve 和 reject 作为参数传递给这个新任务函数</span>
                <span class="token function">doResolve</span><span class="token punctuation">(</span><span class="token function">then</span><span class="token punctuation">.</span><span class="token function">bind</span><span class="token punctuation">(</span>result<span class="token punctuation">)</span><span class="token punctuation">,</span> resolve<span class="token punctuation">,</span> reject<span class="token punctuation">)</span>
                <span class="token keyword">return</span> <span class="token comment">// 结束</span>
            <span class="token punctuation">}</span>

            <span class="token comment">// 如果没有新的任务或者只是返回一个普通的函数，那就让当前的任务直接成功即可。</span>
            <span class="token function">fulfill</span><span class="token punctuation">(</span>result<span class="token punctuation">)</span>
        <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span>error<span class="token punctuation">)</span> <span class="token punctuation">{</span>
           <span class="token comment">// 执行过程中报错了，那就直接失败</span>
           <span class="token function">reject</span><span class="token punctuation">(</span>error<span class="token punctuation">)</span> 
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>

    <span class="token comment">// 拿到新的promise对象中的then</span>
    <span class="token keyword">function</span> <span class="token function">getThen</span> <span class="token punctuation">(</span><span class="token parameter">value</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">const</span> t <span class="token operator">=</span> <span class="token keyword">typeof</span> value
        <span class="token keyword">if</span> <span class="token punctuation">(</span>value <span class="token operator">&amp;&amp;</span> <span class="token punctuation">[</span><span class="token string">'object'</span><span class="token punctuation">,</span> <span class="token string">'function'</span><span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">includes</span><span class="token punctuation">(</span>t<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token comment">// 如果value 是一个promise对象，那就返回这个promise对象的then</span>
            <span class="token keyword">const</span> then <span class="token operator">=</span> value<span class="token punctuation">.</span>then
            <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token string">'function'</span> <span class="token operator">===</span> <span class="token keyword">typeof</span> then<span class="token punctuation">)</span> <span class="token punctuation">{</span>
                <span class="token keyword">return</span> then
            <span class="token punctuation">}</span>
            <span class="token comment">// 如果value是一个普通的函数，走下面的null逻辑，因为普通函数不用对状态进行干预了。</span>
        <span class="token punctuation">}</span>
        <span class="token comment">// value 为空，就返回null</span>
        <span class="token keyword">return</span> <span class="token keyword">null</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token comment">// 执行任务函数promise的then函数</span>
    <span class="token keyword">function</span> <span class="token function">doResolve</span> <span class="token punctuation">(</span><span class="token parameter">fn<span class="token punctuation">,</span> onFulfilled<span class="token punctuation">,</span> onRejected</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">let</span> done <span class="token operator">=</span> <span class="token boolean">false</span> <span class="token comment">// 默认状态为暂未执行成功，也是加个锁</span>
        <span class="token keyword">try</span> <span class="token punctuation">{</span>
            <span class="token comment">// 调用任务函数</span>
            <span class="token function">fn</span><span class="token punctuation">(</span>
                <span class="token comment">// 第一个回调</span>
                <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token parameter">value</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
                    <span class="token keyword">if</span><span class="token punctuation">(</span>done<span class="token punctuation">)</span> <span class="token keyword">return</span><span class="token punctuation">;</span> <span class="token comment">// 异步任务执行完毕，就没必要往下走了。因为任务函数中的两个函数有一个已经执行完了</span>
                    done <span class="token operator">=</span> <span class="token boolean">true</span><span class="token punctuation">;</span>
                    <span class="token function">onFulfilled</span><span class="token punctuation">(</span>value<span class="token punctuation">)</span>
                <span class="token punctuation">}</span><span class="token punctuation">,</span> 
                <span class="token comment">// 第二个回调</span>
                <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token parameter">reason</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
                    <span class="token keyword">if</span> <span class="token punctuation">(</span>done<span class="token punctuation">)</span> <span class="token keyword">return</span><span class="token punctuation">;</span> <span class="token comment">// 异步任务执行完毕，就没必要往下走了。因为任务函数中的两个函数有一个已经执行完了</span>
                    done <span class="token operator">=</span> <span class="token boolean">true</span><span class="token punctuation">;</span>
                    <span class="token function">onRejected</span><span class="token punctuation">(</span>reason<span class="token punctuation">)</span>
                <span class="token punctuation">}</span>
            <span class="token punctuation">)</span>
        <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span>error<span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token keyword">if</span><span class="token punctuation">(</span>done<span class="token punctuation">)</span> <span class="token keyword">return</span>
            done <span class="token operator">=</span> <span class="token boolean">true</span>
            <span class="token function">onRejected</span><span class="token punctuation">(</span>error<span class="token punctuation">)</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>

    <span class="token comment">// 向外暴露的then收集的任务都会被二次封装，才会放入handlers或者直接执行掉</span>
    <span class="token keyword">function</span> <span class="token function">handle</span> <span class="token punctuation">(</span><span class="token parameter">handler</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span>state <span class="token operator">===</span> <span class="token constant">PENDING</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            handlers<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>handler<span class="token punctuation">)</span>
            <span class="token keyword">return</span>
        <span class="token punctuation">}</span>

        <span class="token keyword">if</span> <span class="token punctuation">(</span>state <span class="token operator">===</span> <span class="token constant">FULFILLED</span> <span class="token operator">&amp;&amp;</span> <span class="token string">'function'</span> <span class="token operator">===</span> <span class="token keyword">typeof</span> handler<span class="token punctuation">.</span>onFulfilled<span class="token punctuation">)</span> <span class="token punctuation">{</span>
            handler<span class="token punctuation">.</span><span class="token function">onFulfilled</span><span class="token punctuation">(</span>value<span class="token punctuation">)</span>
        <span class="token punctuation">}</span>

        <span class="token keyword">if</span> <span class="token punctuation">(</span>state <span class="token operator">===</span> <span class="token constant">REJECTED</span> <span class="token operator">&amp;&amp;</span> <span class="token string">'function'</span> <span class="token operator">===</span> <span class="token keyword">typeof</span> handler<span class="token punctuation">.</span>onRejected<span class="token punctuation">)</span> <span class="token punctuation">{</span>
            handler<span class="token punctuation">.</span><span class="token function">onRejected</span><span class="token punctuation">(</span>value<span class="token punctuation">)</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function-variable function">done</span> <span class="token operator">=</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token parameter">onFulfilled<span class="token punctuation">,</span> onRejected</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token comment">// 让then中传递过来的内容异步的去执行</span>
        <span class="token function">setTimeout</span><span class="token punctuation">(</span><span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
            <span class="token function">handle</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
                onFulfilled<span class="token punctuation">,</span>
                onRejected
            <span class="token punctuation">}</span><span class="token punctuation">)</span>
        <span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span>
    <span class="token punctuation">}</span>

    <span class="token comment">// then中的任务会进行二次封装，封装成promise对象，在Promise中会执行上面done</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function-variable function">then</span> <span class="token operator">=</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token parameter">onFulfilled<span class="token punctuation">,</span> onRejected</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">const</span> self <span class="token operator">=</span> <span class="token keyword">this</span><span class="token punctuation">;</span>
        <span class="token keyword">return</span> <span class="token keyword">new</span> <span class="token class-name">Promise</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token parameter">resolve<span class="token punctuation">,</span> reject</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token keyword">return</span> self<span class="token punctuation">.</span><span class="token function">done</span><span class="token punctuation">(</span>
                <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token parameter">result</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
                    <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token string">'function'</span> <span class="token operator">===</span> <span class="token keyword">typeof</span> onFulfilled<span class="token punctuation">)</span> <span class="token punctuation">{</span>
                        <span class="token keyword">try</span> <span class="token punctuation">{</span>
                            <span class="token keyword">return</span> <span class="token function">resolve</span><span class="token punctuation">(</span><span class="token function">onFulfilled</span><span class="token punctuation">(</span>result<span class="token punctuation">)</span><span class="token punctuation">)</span>
                        <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span>error<span class="token punctuation">)</span> <span class="token punctuation">{</span>
                            <span class="token keyword">return</span> <span class="token function">reject</span><span class="token punctuation">(</span>error<span class="token punctuation">)</span>
                        <span class="token punctuation">}</span>
                    <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
                        <span class="token keyword">return</span> <span class="token function">resolve</span><span class="token punctuation">(</span>result<span class="token punctuation">)</span>
                    <span class="token punctuation">}</span>
                <span class="token punctuation">}</span><span class="token punctuation">,</span>
                <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token parameter">error</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
                    <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token string">'fcuntion'</span> <span class="token operator">===</span> <span class="token keyword">typeof</span> onRejected<span class="token punctuation">)</span> <span class="token punctuation">{</span>
                        <span class="token keyword">try</span> <span class="token punctuation">{</span>
                            <span class="token keyword">return</span> <span class="token function">resolve</span><span class="token punctuation">(</span><span class="token function">onRejected</span><span class="token punctuation">(</span>error<span class="token punctuation">)</span><span class="token punctuation">)</span>
                        <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span>error<span class="token punctuation">)</span> <span class="token punctuation">{</span>
                            <span class="token keyword">return</span> <span class="token function">reject</span><span class="token punctuation">(</span>error<span class="token punctuation">)</span>
                        <span class="token punctuation">}</span>
                    <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
                        <span class="token keyword">return</span> <span class="token function">reject</span><span class="token punctuation">(</span>error<span class="token punctuation">)</span>
                    <span class="token punctuation">}</span>
                <span class="token punctuation">}</span>
            <span class="token punctuation">)</span>
        <span class="token punctuation">}</span><span class="token punctuation">)</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre></div><h2 id="总结"><a href="#总结" class="header-anchor">#</a> 总结</h2> <p>面试过程中 JS的问题相对于CSS来说会难点，掌握以上的内容，JS基础的应用是没啥问题的。</p> <p>和HTML DOM 和网络有关的 HTTP、NodeJS呀，和JS构建工具有关的Webpack呀，还有较为常用的JS超集TypeScript等等。<br>
耽搁了耽搁了【狗头】，后面有兴趣有精力了，继续更新。</p> <p>本篇文章说了JS面试过程中对个人基础(非框架)方面的较常见也最实用的一些内容。</p></div></div> <div class="page-edit"><!----> <div class="tags"><a href="/tags/?tag=%E9%9D%A2%E8%AF%95" title="标签">#面试</a><a href="/tags/?tag=JS" title="标签">#JS</a><a href="/tags/?tag=NodeJs" title="标签">#NodeJs</a><a href="/tags/?tag=HTTP" title="标签">#HTTP</a><a href="/tags/?tag=Webpack" title="标签">#Webpack</a></div> <div class="last-updated"><span class="prefix">上次更新时间:</span> <span class="time">10年18月2023日 01时57分53秒</span></div></div> <div class="page-nav-wapper"><div class="page-nav-centre-wrap"><a href="/pages/14337520552/" class="page-nav-centre page-nav-centre-prev"><div class="tooltip">玩转前端面试之CSS</div></a> <a href="/pages/73600130213/" class="page-nav-centre page-nav-centre-next"><div class="tooltip">Bash简介</div></a></div> <div class="page-nav"><p class="inner"><span class="prev">
        ←
        <a href="/pages/14337520552/" class="prev">玩转前端面试之CSS</a></span> <span class="next"><a href="/pages/73600130213/"> Bash简介 </a>
        →
      </span></p></div></div></div> <div class="article-list"><div class="article-title"><a href="/archives/" class="iconfont icon-bi">最近更新</a></div> <div class="article-wrapper"><dl><dd>01</dd> <dt><a href="/pages/45343271027/"><div>01.数据结构导论一览.md</div></a> <span>10-16</span></dt></dl><dl><dd>02</dd> <dt><a href="/pages/38850370637/"><div>30.2023年06月04日.md</div></a> <span>06-04</span></dt></dl><dl><dd>03</dd> <dt><a href="/pages/74707370537/"><div>08.与测量相关.md</div></a> <span>05-06</span></dt></dl> <dl><dd></dd> <dt><a href="/archives/" class="more">更多文章&gt;</a></dt></dl></div></div> </main></div> <div class="footer"><!----> 
  Theme by
  <a href="https://github.com/xugaoyi/vuepress-theme-vdoing" target="_blank" title="本站主题">Vdoing</a> 
    | Copyright © 2017-2023
    <span class="link">aiyoudiao 码二</span> <span>备案号：</span> <a href="https://beian.miit.gov.cn/#/Integrated/index" target="_blank" title="备案号">鄂ICP备2022002654号-1</a></div> <div class="buttons"><div title="返回顶部" class="button blur go-to-top iconfont icon-fanhuidingbu" style="display:none;"></div> <div title="去评论" class="button blur go-to-comment iconfont icon-pinglun" style="display:none;"></div></div> <!----> <!----> <!----></div><div class="global-ui"><div></div><APlayer audio="" fixed="true" theme="#b7daff" loop="loop" order="list" preload="auto" volume="0.7" mutex="true" lrc-type="3" list-max-height="250" storage-name="vuepress-plugin-meting" id="aplayer-fixed"></APlayer><div id="VuepressPluginLive2d"></div></div></div>
    <script src="/assets/js/app.bd2fbc77.js" defer></script><script src="/assets/js/3.72c9c947.js" defer></script><script src="/assets/js/100.20d2348f.js" defer></script><script src="/assets/js/42.4251ca36.js" defer></script>
  </body>
</html>
